Skip to contentSkip to conversation

vfio: blacklist legacy virtio devices

PATCH In review Michael S. Tsirkin 4 participants 15 messages Aug 30, 2016

Subsystems (3)
lore
Share & tools
lore

Michael S. Tsirkin

author maintainer
#0 lore raw

v2 cover vfio: blacklist legacy virtio devices

Legacy virtio devices always bypassed an IOMMU, so using them with vfio was never safe. This adds a quirk detecting these and disabling VFIO unless the noiommu mode is used. At the moment, this only applies to virtio-pci devices.

The patch might make sense on stable as well.

Michael S. Tsirkin (2):
vfio: report group noiommu status
vfio: add virtio pci quirk

 drivers/vfio/pci/vfio_pci_private.h |   1 +
 include/linux/vfio.h                |   2 +
 drivers/vfio/pci/vfio_pci.c         |  14 ++++
 drivers/vfio/pci/vfio_pci_virtio.c  | 140 ++++++++++++++++++++++++++++++++++++
 drivers/vfio/vfio.c                 |  12 ++++
 drivers/vfio/pci/Makefile           |   1 +
 6 files changed, 170 insertions(+)
 create mode 100644 drivers/vfio/pci/vfio_pci_virtio.c
Signature · 1 line
MST
author maintainer
#1 lore raw

PATCH v2 1/2 vfio: report group noiommu status

When using vfio, callers might want to know whether device is added to a regular group or an non-iommu group.

Report this status from vfio_is_noiommu_group_dev.

---
 include/linux/vfio.h |  2 ++
 drivers/vfio/vfio.c  | 12 ++++++++++++
 2 files changed, 14 insertions(+)
include/linux/vfio.h +2 -0
index 0ecae0b..584757b 100644
@@ -51,6 +51,8 @@ extern int vfio_add_group_dev(struct device *dev,
 			      const struct vfio_device_ops *ops,
 			      void *device_data);
 
+extern bool vfio_is_noiommu_group_dev(struct device *dev);
+
 extern void *vfio_del_group_dev(struct device *dev);
 extern struct vfio_device *vfio_device_get_from_dev(struct device *dev);
 extern void vfio_device_put(struct vfio_device *device);
drivers/vfio/vfio.c +12 -0
index d1d70e0..22f279f 100644
@@ -872,6 +872,18 @@ static bool vfio_dev_present(struct vfio_group *group, struct device *dev)
 }
 
 /*
+ * Is device part of a noiommu group?
+ * Note: must call vfio_add_group_dev first.
+ */
+bool vfio_is_noiommu_group_dev(struct device *dev)
+{
+	struct vfio_device *device = dev_get_drvdata(dev);
+	struct vfio_group *group = device->group;
+
+	return group->noiommu;
+}
+
+/*
  * Decrement the device reference count and wait for the device to be
  * removed.  Open file descriptors for the device... */
 void *vfio_del_group_dev(struct device *dev)
Signature · 1 line
MST
author maintainer
#2 lore raw

PATCH v2 2/2 vfio: add virtio pci quirk

In reply to Michael S. Tsirkin

Modern virtio pci devices can set VIRTIO_F_IOMMU_PLATFORM to signal they are safe to use with an IOMMU.

Without this bit, exposing the device to userspace is unsafe, so probe and fail VFIO initialization unless noiommu is enabled.

---
 drivers/vfio/pci/vfio_pci_private.h |   1 +
 drivers/vfio/pci/vfio_pci.c         |  14 ++++
 drivers/vfio/pci/vfio_pci_virtio.c  | 140 ++++++++++++++++++++++++++++++++++++
 drivers/vfio/pci/Makefile           |   1 +
 4 files changed, 156 insertions(+)
 create mode 100644 drivers/vfio/pci/vfio_pci_virtio.c
drivers/vfio/pci/vfio_pci_private.h +1 -0
index 2128de8..2bd5616 100644
@@ -139,4 +139,5 @@ static inline int vfio_pci_igd_init(struct vfio_pci_device *vdev)
 	return -ENODEV;
 }
 #endif
+extern int vfio_pci_virtio_quirk(struct vfio_pci_device *vdev, bool noiommu);
 #endif /* VFIO_PCI_PRIVATE_H */
drivers/vfio/pci/vfio_pci.c +14 -0
index d624a52..e93bf0c 100644
@@ -1236,6 +1236,20 @@ static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 		return ret;
 	}
 
+	if (pdev->vendor == PCI_VENDOR_ID_REDHAT_QUMRANET) {
+		bool noiommu = vfio_is_noiommu_group_dev(&pdev->dev);
+
+		ret = vfio_pci_virtio_quirk(vdev, noiommu);
+		if (ret) {
+			dev_warn(&vdev->pdev->dev,
+				 "Failed to setup Virtio for VFIO\n");
+			vfio_del_group_dev(&pdev->dev);
+			vfio_iommu_group_put(group, &pdev->dev);
+			kfree(vdev);
+			return ret;
+		}
+	}
+
 	if (vfio_pci_is_vga(pdev)) {
 		vga_client_register(pdev, vdev, NULL, vfio_pci_set_vga_decode);
 		vga_set_legacy_decoding(pdev,
drivers/vfio/pci/vfio_pci_virtio.c new file +140 -0

140 lines not on this page · raw message

drivers/vfio/pci/Makefile +1 -0
index 76d8ec0..e9b20e7 100644
@@ -1,5 +1,6 @@
 
 vfio-pci-y := vfio_pci.o vfio_pci_intrs.o vfio_pci_rdwr.o vfio_pci_config.o
+vfio-pci-y += vfio_pci_virtio.o
 vfio-pci-$(CONFIG_VFIO_PCI_IGD) += vfio_pci_igd.o
 
 obj-$(CONFIG_VFIO_PCI) += vfio-pci.o
Signature · 1 line
MST
#13 lore raw
In reply to Michael S. Tsirkin
Quoted 2 lines · Michael S. Tsirkin

Legacy virtio devices always bypassed an IOMMU, so using them with vfio was never safe.

And it actually won't work since GPA is assumed in the device. So I'm not sure this is must since we should get a IOMMU fault in this case.

Quoted 9 lines

This adds a quirk detecting these and disabling VFIO unless the
noiommu mode is used. At the moment, this only applies to virtio-pci devices.

The patch might make sense on stable as well.

Michael S. Tsirkin (2):
vfio: report group noiommu status
vfio: add virtio pci quirk

Quoted 9 lines
  drivers/vfio/pci/vfio_pci_private.h |   1 +
  include/linux/vfio.h                |   2 +
  drivers/vfio/pci/vfio_pci.c         |  14 ++++
  drivers/vfio/pci/vfio_pci_virtio.c  | 140 ++++++++++++++++++++++++++++++++++++
  drivers/vfio/vfio.c                 |  12 ++++
  drivers/vfio/pci/Makefile           |   1 +
  6 files changed, 170 insertions(+)

create mode 100644 drivers/vfio/pci/vfio_pci_virtio.c

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#3 lore raw

Re: [PATCH v2 2/2] vfio: add virtio pci quirk

In reply to Michael S. Tsirkin
Quoted 30 lines · On Tue, 30 Aug 2016 05:27:17 +0300 "Michael S. Tsirkin" <[email protected]> wrote:

Modern virtio pci devices can set VIRTIO_F_IOMMU_PLATFORM to signal they are safe to use with an IOMMU.

Without this bit, exposing the device to userspace is unsafe, so probe and fail VFIO initialization unless noiommu is enabled.

---
 drivers/vfio/pci/vfio_pci_private.h |   1 +
 drivers/vfio/pci/vfio_pci.c         |  14 ++++
 drivers/vfio/pci/vfio_pci_virtio.c  | 140 ++++++++++++++++++++++++++++++++++++
 drivers/vfio/pci/Makefile           |   1 +
 4 files changed, 156 insertions(+)
 create mode 100644 drivers/vfio/pci/vfio_pci_virtio.c
drivers/vfio/pci/vfio_pci_private.h +1 -0
index 2128de8..2bd5616 100644
@@ -139,4 +139,5 @@ static inline int vfio_pci_igd_init(struct vfio_pci_device *vdev)
 	return -ENODEV;
 }
 #endif
+extern int vfio_pci_virtio_quirk(struct vfio_pci_device *vdev, bool noiommu);
 #endif /* VFIO_PCI_PRIVATE_H */
drivers/vfio/pci/vfio_pci.c +0 -0
index d624a52..e93bf0c 100644

drivers/vfio/pci/vfio_pci.c static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 1239

Quoted 4 lines
drivers/vfio/pci/vfio_pci.c +1 -0
@@ -1236,6 +1236,20 @@ static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 		return ret;
 	}
 
+	if (pdev->vendor == PCI_VENDOR_ID_REDHAT_QUMRANET) {

Perhaps a vfio_pci_is_virtio() like vga below? Let's test the device ID range initially as well, this test raised a big red flag for me whether all devices within this vendor ID were virtio.

drivers/vfio/pci/vfio_pci.c static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 1240

Quoted 1 line
drivers/vfio/pci/vfio_pci.c +1 -0
+		bool noiommu = vfio_is_noiommu_group_dev(&pdev->dev);

I think you can use iommu_present() for this and avoid patch 1of2. noiommu is mutually exclusive to an iommu being present. Seems like all of this logic should be in the quirk itself, I'm not sure what it buys to get the value here but wait until later to use it. Using iommu_present() could also move this test much earlier in vfio_pci_probe() making the exit path easier.

Quoted 21 lines
drivers/vfio/pci/vfio_pci.c +12 -0
+
+		ret = vfio_pci_virtio_quirk(vdev, noiommu);
+		if (ret) {
+			dev_warn(&vdev->pdev->dev,
+				 "Failed to setup Virtio for VFIO\n");
+			vfio_del_group_dev(&pdev->dev);
+			vfio_iommu_group_put(group, &pdev->dev);
+			kfree(vdev);
+			return ret;
+		}
+	}
+
 	if (vfio_pci_is_vga(pdev)) {
 		vga_client_register(pdev, vdev, NULL, vfio_pci_set_vga_decode);
 		vga_set_legacy_decoding(pdev,
drivers/vfio/pci/vfio_pci_virtio.c new file +0 -0
new file mode 100644
index 0000000..e1ecffd

drivers/vfio/pci/vfio_pci_virtio.c line 2

Quoted 2 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +2 -0
@@ -0,0 +1,140 @@
+/*
+ * VFIO PCI Intel Graphics support
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

drivers/vfio/pci/vfio_pci_virtio.c line 5

Quoted 3 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +3 -0
+ *
+ * Copyright (C) 2016 Red Hat, Inc.  All rights reserved.
+ *	Author: Alex Williamson <[email protected]>

Update.

drivers/vfio/pci/vfio_pci_virtio.c line 18

Quoted 13 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +13 -0
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * Register a device specific region through which to provide read-only
+ * access to the Intel IGD opregion.  The register defining the opregion
+ * address is also virtualized to prevent user modification.
+ */
+
+#include <linux/io.h>
+#include <linux/pci.h>
+#include <linux/uaccess.h>

Are io.h and uaccess.h needed?

Quoted 4 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +4 -0
+#include <linux/vfio.h>
+#include <linux/virtio_pci.h>
+#include <linux/virtio_config.h>
+

drivers/vfio/pci/vfio_pci_virtio.c line 32

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+#include "vfio_pci_private.h"
+
+/**
+ * virtio_pci_find_capability - walk capabilities to find device info.
+ * @dev: the pci device
+ * @cfg_type: the VIRTIO_PCI_CAP_* value we seek
+ *
+ * Returns offset of the capability, or 0.
+ */
+static inline int virtio_pci_find_capability(struct pci_dev *dev, u8 cfg_type)

Does inlining this really make sense?

Quoted 13 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +13 -0
+{
+	int pos;
+
+	for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR);
+	     pos > 0;
+	     pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
+		u8 type;
+		pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
+							 cfg_type),
+				     &type);
+
+		if (type != cfg_type)
+			continue;

drivers/vfio/pci/vfio_pci_virtio.c line 55

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+
+		/* Ignore structures with reserved BAR values */
+		if (type != VIRTIO_PCI_CAP_PCI_CFG) {
+			u8 bar;
+
+			pci_read_config_byte(dev, pos +
+					     offsetof(struct virtio_pci_cap,
+						      bar),
+					     &bar);
+			if (bar > 0x5)

s/0x5/PCI_STD_RESOURCE_END/

drivers/vfio/pci/vfio_pci_virtio.c line 67

Quoted 12 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +12 -0
+				continue;
+		}
+
+		return pos;
+	}
+	return 0;
+}
+
+
+int vfio_pci_virtio_quirk(struct vfio_pci_device *vdev, bool noiommu)
+{
+	struct pci_dev *dev = vdev->pdev;

Please use *pdev for consistency with the rest of drivers/vfio/pci/*

Also, is there any reason to pass the vfio_pci_device? There's nothing vfio here otherwise and we could remove more #includes.

drivers/vfio/pci/vfio_pci_virtio.c line 79

Quoted 12 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +12 -0
+	int common, cfg;
+	u32 features;
+	u32 offset;
+	u8 bar;
+
+	/* Without an IOMMU, we don't care */
+	if (noiommu)
+		return 0;
+
+        /* Virtio only owns devices >= 0x1000 and <= 0x107f: leave the rest. */
+        if (dev->device < 0x1000 || dev->device > 0x107f)
+                return 0;

Whitespace

Quoted 6 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +6 -0
+
+	/* Check whether device enforces the IOMMU correctly */
+
+	/*
+	 * All modern devices must have common and cfg capabilities. We use cfg
+	 * capability for access so that we don't need to worry about resource

drivers/vfio/pci/vfio_pci_virtio.c line 95

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+	 * availability. Slow but sure.
+	 * Note that all vendor-specific fields we access are little-endian
+	 * which matches what pci config accessors expect, so they do byteswap
+	 * for us if appropriate.
+	 */
+	common = virtio_pci_find_capability(dev, VIRTIO_PCI_CAP_COMMON_CFG);
+	cfg = virtio_pci_find_capability(dev, VIRTIO_PCI_CAP_PCI_CFG);
+	if (!cfg || !common) {
+                dev_warn(&dev->dev,
+                         "Virtio device lacks common or pci cfg.\n");

Whitespace

Quoted 30 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +30 -0
+		return -ENODEV;
+	}
+
+	pci_read_config_byte(dev, common + offsetof(struct virtio_pci_cap,
+						    bar),
+			     &bar);
+	pci_read_config_dword(dev, common + offsetof(struct virtio_pci_cap,
+						    offset),
+			     &offset);
+
+	/* Program cfg capability for dword access into common cfg. */
+	pci_write_config_byte(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  cap.bar),
+			      bar);
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						   cap.length),
+			       0x4);
+
+	/* Select features dword that has VIRTIO_F_IOMMU_PLATFORM. */
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  cap.offset),
+			       offset + offsetof(struct virtio_pci_common_cfg,
+						 device_feature_select));
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  pci_cfg_data),
+			       VIRTIO_F_IOMMU_PLATFORM / 32);
+
+	/* Get the features dword. */
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  cap.offset),

drivers/vfio/pci/vfio_pci_virtio.c line 135

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+			       offset + offsetof(struct virtio_pci_common_cfg,
+						 device_feature));
+	pci_read_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  pci_cfg_data),
+			      &features);
+
+	/* Does this device obey the platform's IOMMU? If not it's an error. */
+	if (!(features & (0x1 << (VIRTIO_F_IOMMU_PLATFORM % 32)))) {
+                dev_warn(&dev->dev,
+                         "Virtio device lacks VIRTIO_F_IOMMU_PLATFORM.\n");

Whitespace

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +5 -0
+		return -ENODEV;
+	}
+
+	return 0;
+}
drivers/vfio/pci/Makefile +0 -0
index 76d8ec0..e9b20e7 100644

drivers/vfio/pci/Makefile line 6

Quoted 6 lines
drivers/vfio/pci/Makefile +1 -0
@@ -1,5 +1,6 @@
 
 vfio-pci-y := vfio_pci.o vfio_pci_intrs.o vfio_pci_rdwr.o vfio_pci_config.o
+vfio-pci-y += vfio_pci_virtio.o
 vfio-pci-$(CONFIG_VFIO_PCI_IGD) += vfio_pci_igd.o
 
 obj-$(CONFIG_VFIO_PCI) += vfio-pci.o

Thanks,
Alex

author maintainer
#4 lore raw

Re: [PATCH v2 2/2] vfio: add virtio pci quirk

Quoted 42 lines · Alex Williamson
Quoted 30 lines · On Tue, 30 Aug 2016 05:27:17 +0300 "Michael S. Tsirkin" <[email protected]> wrote:

Modern virtio pci devices can set VIRTIO_F_IOMMU_PLATFORM to signal they are safe to use with an IOMMU.

Without this bit, exposing the device to userspace is unsafe, so probe and fail VFIO initialization unless noiommu is enabled.

---
 drivers/vfio/pci/vfio_pci_private.h |   1 +
 drivers/vfio/pci/vfio_pci.c         |  14 ++++
 drivers/vfio/pci/vfio_pci_virtio.c  | 140 ++++++++++++++++++++++++++++++++++++
 drivers/vfio/pci/Makefile           |   1 +
 4 files changed, 156 insertions(+)
 create mode 100644 drivers/vfio/pci/vfio_pci_virtio.c
drivers/vfio/pci/vfio_pci_private.h +1 -0
index 2128de8..2bd5616 100644
@@ -139,4 +139,5 @@ static inline int vfio_pci_igd_init(struct vfio_pci_device *vdev)
 	return -ENODEV;
 }
 #endif
+extern int vfio_pci_virtio_quirk(struct vfio_pci_device *vdev, bool noiommu);
 #endif /* VFIO_PCI_PRIVATE_H */
drivers/vfio/pci/vfio_pci.c +0 -0
index d624a52..e93bf0c 100644

drivers/vfio/pci/vfio_pci.c static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 1239

Quoted 4 lines
drivers/vfio/pci/vfio_pci.c +1 -0
@@ -1236,6 +1236,20 @@ static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 		return ret;
 	}
 
+	if (pdev->vendor == PCI_VENDOR_ID_REDHAT_QUMRANET) {

Perhaps a vfio_pci_is_virtio() like vga below? Let's test the device ID range initially as well, this test raised a big red flag for me whether all devices within this vendor ID were virtio.

Quoted 3 lines

drivers/vfio/pci/vfio_pci.c static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 1240

Quoted 1 line
drivers/vfio/pci/vfio_pci.c +1 -0
+		bool noiommu = vfio_is_noiommu_group_dev(&pdev->dev);

I think you can use iommu_present() for this and avoid patch 1of2.

So in presence of an IOMMU in the system, is it impossible to bind the noiommu device?

I did not test this yet.

If yes this is something we'll need to fix as well - we do want to allow binding noiommu to a legacy virtio device.

Quoted 209 lines

noiommu is mutually exclusive to an iommu being present. Seems like all of this logic should be in the quirk itself, I'm not sure what it buys to get the value here but wait until later to use it. Using iommu_present() could also move this test much earlier in vfio_pci_probe() making the exit path easier.

Quoted 21 lines
drivers/vfio/pci/vfio_pci.c +12 -0
+
+		ret = vfio_pci_virtio_quirk(vdev, noiommu);
+		if (ret) {
+			dev_warn(&vdev->pdev->dev,
+				 "Failed to setup Virtio for VFIO\n");
+			vfio_del_group_dev(&pdev->dev);
+			vfio_iommu_group_put(group, &pdev->dev);
+			kfree(vdev);
+			return ret;
+		}
+	}
+
 	if (vfio_pci_is_vga(pdev)) {
 		vga_client_register(pdev, vdev, NULL, vfio_pci_set_vga_decode);
 		vga_set_legacy_decoding(pdev,
drivers/vfio/pci/vfio_pci_virtio.c new file +0 -0
new file mode 100644
index 0000000..e1ecffd

drivers/vfio/pci/vfio_pci_virtio.c line 2

Quoted 2 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +2 -0
@@ -0,0 +1,140 @@
+/*
+ * VFIO PCI Intel Graphics support
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

drivers/vfio/pci/vfio_pci_virtio.c line 5

Quoted 3 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +3 -0
+ *
+ * Copyright (C) 2016 Red Hat, Inc.  All rights reserved.
+ *	Author: Alex Williamson <[email protected]>

Update.

drivers/vfio/pci/vfio_pci_virtio.c line 18

Quoted 13 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +13 -0
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * Register a device specific region through which to provide read-only
+ * access to the Intel IGD opregion.  The register defining the opregion
+ * address is also virtualized to prevent user modification.
+ */
+
+#include <linux/io.h>
+#include <linux/pci.h>
+#include <linux/uaccess.h>

Are io.h and uaccess.h needed?

Quoted 4 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +4 -0
+#include <linux/vfio.h>
+#include <linux/virtio_pci.h>
+#include <linux/virtio_config.h>
+

drivers/vfio/pci/vfio_pci_virtio.c line 32

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+#include "vfio_pci_private.h"
+
+/**
+ * virtio_pci_find_capability - walk capabilities to find device info.
+ * @dev: the pci device
+ * @cfg_type: the VIRTIO_PCI_CAP_* value we seek
+ *
+ * Returns offset of the capability, or 0.
+ */
+static inline int virtio_pci_find_capability(struct pci_dev *dev, u8 cfg_type)

Does inlining this really make sense?

Quoted 13 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +13 -0
+{
+	int pos;
+
+	for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR);
+	     pos > 0;
+	     pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
+		u8 type;
+		pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
+							 cfg_type),
+				     &type);
+
+		if (type != cfg_type)
+			continue;

drivers/vfio/pci/vfio_pci_virtio.c line 55

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+
+		/* Ignore structures with reserved BAR values */
+		if (type != VIRTIO_PCI_CAP_PCI_CFG) {
+			u8 bar;
+
+			pci_read_config_byte(dev, pos +
+					     offsetof(struct virtio_pci_cap,
+						      bar),
+					     &bar);
+			if (bar > 0x5)

s/0x5/PCI_STD_RESOURCE_END/

drivers/vfio/pci/vfio_pci_virtio.c line 67

Quoted 12 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +12 -0
+				continue;
+		}
+
+		return pos;
+	}
+	return 0;
+}
+
+
+int vfio_pci_virtio_quirk(struct vfio_pci_device *vdev, bool noiommu)
+{
+	struct pci_dev *dev = vdev->pdev;

Please use *pdev for consistency with the rest of drivers/vfio/pci/*

Also, is there any reason to pass the vfio_pci_device? There's nothing vfio here otherwise and we could remove more #includes.

drivers/vfio/pci/vfio_pci_virtio.c line 79

Quoted 12 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +12 -0
+	int common, cfg;
+	u32 features;
+	u32 offset;
+	u8 bar;
+
+	/* Without an IOMMU, we don't care */
+	if (noiommu)
+		return 0;
+
+        /* Virtio only owns devices >= 0x1000 and <= 0x107f: leave the rest. */
+        if (dev->device < 0x1000 || dev->device > 0x107f)
+                return 0;

Whitespace

Quoted 6 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +6 -0
+
+	/* Check whether device enforces the IOMMU correctly */
+
+	/*
+	 * All modern devices must have common and cfg capabilities. We use cfg
+	 * capability for access so that we don't need to worry about resource

drivers/vfio/pci/vfio_pci_virtio.c line 95

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+	 * availability. Slow but sure.
+	 * Note that all vendor-specific fields we access are little-endian
+	 * which matches what pci config accessors expect, so they do byteswap
+	 * for us if appropriate.
+	 */
+	common = virtio_pci_find_capability(dev, VIRTIO_PCI_CAP_COMMON_CFG);
+	cfg = virtio_pci_find_capability(dev, VIRTIO_PCI_CAP_PCI_CFG);
+	if (!cfg || !common) {
+                dev_warn(&dev->dev,
+                         "Virtio device lacks common or pci cfg.\n");

Whitespace

Quoted 30 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +30 -0
+		return -ENODEV;
+	}
+
+	pci_read_config_byte(dev, common + offsetof(struct virtio_pci_cap,
+						    bar),
+			     &bar);
+	pci_read_config_dword(dev, common + offsetof(struct virtio_pci_cap,
+						    offset),
+			     &offset);
+
+	/* Program cfg capability for dword access into common cfg. */
+	pci_write_config_byte(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  cap.bar),
+			      bar);
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						   cap.length),
+			       0x4);
+
+	/* Select features dword that has VIRTIO_F_IOMMU_PLATFORM. */
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  cap.offset),
+			       offset + offsetof(struct virtio_pci_common_cfg,
+						 device_feature_select));
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  pci_cfg_data),
+			       VIRTIO_F_IOMMU_PLATFORM / 32);
+
+	/* Get the features dword. */
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  cap.offset),

drivers/vfio/pci/vfio_pci_virtio.c line 135

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+			       offset + offsetof(struct virtio_pci_common_cfg,
+						 device_feature));
+	pci_read_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  pci_cfg_data),
+			      &features);
+
+	/* Does this device obey the platform's IOMMU? If not it's an error. */
+	if (!(features & (0x1 << (VIRTIO_F_IOMMU_PLATFORM % 32)))) {
+                dev_warn(&dev->dev,
+                         "Virtio device lacks VIRTIO_F_IOMMU_PLATFORM.\n");

Whitespace

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +5 -0
+		return -ENODEV;
+	}
+
+	return 0;
+}
drivers/vfio/pci/Makefile +0 -0
index 76d8ec0..e9b20e7 100644

drivers/vfio/pci/Makefile line 6

Quoted 6 lines
drivers/vfio/pci/Makefile +1 -0
@@ -1,5 +1,6 @@
 
 vfio-pci-y := vfio_pci.o vfio_pci_intrs.o vfio_pci_rdwr.o vfio_pci_config.o
+vfio-pci-y += vfio_pci_virtio.o
 vfio-pci-$(CONFIG_VFIO_PCI_IGD) += vfio_pci_igd.o
 
 obj-$(CONFIG_VFIO_PCI) += vfio-pci.o

Thanks,
Alex

author maintainer
#14 lore raw
In reply to Jason Wang
Quoted 8 lines · Jason Wang
Quoted 2 lines · Michael S. Tsirkin

Legacy virtio devices always bypassed an IOMMU, so using them with vfio was never safe.

And it actually won't work since GPA is assumed in the device. So I'm not sure this is must since we should get a IOMMU fault in this case.

We won't get an IOMMU fault for legacy systems since they bypass the IOMMU. Instead guest userspace will get full access to all of guest memory through the device.

Quoted 9 lines
Quoted 9 lines

This adds a quirk detecting these and disabling VFIO unless the
noiommu mode is used. At the moment, this only applies to virtio-pci devices.

The patch might make sense on stable as well.

Michael S. Tsirkin (2):
vfio: report group noiommu status
vfio: add virtio pci quirk

Quoted 9 lines
Quoted 9 lines
  drivers/vfio/pci/vfio_pci_private.h |   1 +
  include/linux/vfio.h                |   2 +
  drivers/vfio/pci/vfio_pci.c         |  14 ++++
  drivers/vfio/pci/vfio_pci_virtio.c  | 140 ++++++++++++++++++++++++++++++++++++
  drivers/vfio/vfio.c                 |  12 ++++
  drivers/vfio/pci/Makefile           |   1 +
  6 files changed, 170 insertions(+)

create mode 100644 drivers/vfio/pci/vfio_pci_virtio.c

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#5 lore raw

Re: [PATCH v2 2/2] vfio: add virtio pci quirk

In reply to Alex Williamson
Quoted 42 lines · On Mon, 29 Aug 2016 21:23:25 -0600 Alex Williamson…
Quoted 30 lines · On Tue, 30 Aug 2016 05:27:17 +0300 "Michael S. Tsirkin" <[email protected]> wrote:

Modern virtio pci devices can set VIRTIO_F_IOMMU_PLATFORM to signal they are safe to use with an IOMMU.

Without this bit, exposing the device to userspace is unsafe, so probe and fail VFIO initialization unless noiommu is enabled.

---
 drivers/vfio/pci/vfio_pci_private.h |   1 +
 drivers/vfio/pci/vfio_pci.c         |  14 ++++
 drivers/vfio/pci/vfio_pci_virtio.c  | 140 ++++++++++++++++++++++++++++++++++++
 drivers/vfio/pci/Makefile           |   1 +
 4 files changed, 156 insertions(+)
 create mode 100644 drivers/vfio/pci/vfio_pci_virtio.c
drivers/vfio/pci/vfio_pci_private.h +1 -0
index 2128de8..2bd5616 100644
@@ -139,4 +139,5 @@ static inline int vfio_pci_igd_init(struct vfio_pci_device *vdev)
 	return -ENODEV;
 }
 #endif
+extern int vfio_pci_virtio_quirk(struct vfio_pci_device *vdev, bool noiommu);
 #endif /* VFIO_PCI_PRIVATE_H */
drivers/vfio/pci/vfio_pci.c +0 -0
index d624a52..e93bf0c 100644

drivers/vfio/pci/vfio_pci.c static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 1239

Quoted 4 lines
drivers/vfio/pci/vfio_pci.c +1 -0
@@ -1236,6 +1236,20 @@ static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 		return ret;
 	}
 
+	if (pdev->vendor == PCI_VENDOR_ID_REDHAT_QUMRANET) {  

Perhaps a vfio_pci_is_virtio() like vga below? Let's test the device ID range initially as well, this test raised a big red flag for me whether all devices within this vendor ID were virtio.

Quoted 8 lines

drivers/vfio/pci/vfio_pci.c static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 1240

Quoted 1 line
drivers/vfio/pci/vfio_pci.c +1 -0
+		bool noiommu = vfio_is_noiommu_group_dev(&pdev->dev);  

I think you can use iommu_present() for this and avoid patch 1of2. noiommu is mutually exclusive to an iommu being present. Seems like all of this logic should be in the quirk itself, I'm not sure what it buys to get the value here but wait until later to use it. Using iommu_present() could also move this test much earlier in vfio_pci_probe() making the exit path easier.

Except then I'm reintroducing the bug fixed by 16ab8a5cbea4 since iommu_present() assumes an IOMMU API based device. I'll try to think if there's another way to avoid adding the is_noiommu function. Thanks,

Alex

Quoted 204 lines
Quoted 22 lines
drivers/vfio/pci/vfio_pci.c +12 -0
+
+		ret = vfio_pci_virtio_quirk(vdev, noiommu);
+		if (ret) {
+			dev_warn(&vdev->pdev->dev,
+				 "Failed to setup Virtio for VFIO\n");
+			vfio_del_group_dev(&pdev->dev);
+			vfio_iommu_group_put(group, &pdev->dev);
+			kfree(vdev);
+			return ret;
+		}
+	}
+
 	if (vfio_pci_is_vga(pdev)) {
 		vga_client_register(pdev, vdev, NULL, vfio_pci_set_vga_decode);
 		vga_set_legacy_decoding(pdev,
drivers/vfio/pci/vfio_pci_virtio.c new file +0 -0
new file mode 100644
index 0000000..e1ecffd

drivers/vfio/pci/vfio_pci_virtio.c line 2

Quoted 2 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +2 -0
@@ -0,0 +1,140 @@
+/*
+ * VFIO PCI Intel Graphics support  
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

drivers/vfio/pci/vfio_pci_virtio.c line 5

Quoted 3 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +3 -0
+ *
+ * Copyright (C) 2016 Red Hat, Inc.  All rights reserved.
+ *	Author: Alex Williamson <[email protected]>  

Update.

drivers/vfio/pci/vfio_pci_virtio.c line 18

Quoted 13 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +13 -0
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * Register a device specific region through which to provide read-only
+ * access to the Intel IGD opregion.  The register defining the opregion
+ * address is also virtualized to prevent user modification.
+ */
+
+#include <linux/io.h>
+#include <linux/pci.h>
+#include <linux/uaccess.h>  

Are io.h and uaccess.h needed?

Quoted 4 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +4 -0
+#include <linux/vfio.h>
+#include <linux/virtio_pci.h>
+#include <linux/virtio_config.h>
+

drivers/vfio/pci/vfio_pci_virtio.c line 32

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+#include "vfio_pci_private.h"
+
+/**
+ * virtio_pci_find_capability - walk capabilities to find device info.
+ * @dev: the pci device
+ * @cfg_type: the VIRTIO_PCI_CAP_* value we seek
+ *
+ * Returns offset of the capability, or 0.
+ */
+static inline int virtio_pci_find_capability(struct pci_dev *dev, u8 cfg_type)  

Does inlining this really make sense?

Quoted 13 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +13 -0
+{
+	int pos;
+
+	for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR);
+	     pos > 0;
+	     pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
+		u8 type;
+		pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
+							 cfg_type),
+				     &type);
+
+		if (type != cfg_type)
+			continue;

drivers/vfio/pci/vfio_pci_virtio.c line 55

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+
+		/* Ignore structures with reserved BAR values */
+		if (type != VIRTIO_PCI_CAP_PCI_CFG) {
+			u8 bar;
+
+			pci_read_config_byte(dev, pos +
+					     offsetof(struct virtio_pci_cap,
+						      bar),
+					     &bar);
+			if (bar > 0x5)  

s/0x5/PCI_STD_RESOURCE_END/

drivers/vfio/pci/vfio_pci_virtio.c line 67

Quoted 12 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +12 -0
+				continue;
+		}
+
+		return pos;
+	}
+	return 0;
+}
+
+
+int vfio_pci_virtio_quirk(struct vfio_pci_device *vdev, bool noiommu)
+{
+	struct pci_dev *dev = vdev->pdev;  

Please use *pdev for consistency with the rest of drivers/vfio/pci/*

Also, is there any reason to pass the vfio_pci_device? There's nothing vfio here otherwise and we could remove more #includes.

drivers/vfio/pci/vfio_pci_virtio.c line 79

Quoted 12 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +12 -0
+	int common, cfg;
+	u32 features;
+	u32 offset;
+	u8 bar;
+
+	/* Without an IOMMU, we don't care */
+	if (noiommu)
+		return 0;
+
+        /* Virtio only owns devices >= 0x1000 and <= 0x107f: leave the rest. */
+        if (dev->device < 0x1000 || dev->device > 0x107f)
+                return 0;  

Whitespace

Quoted 6 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +6 -0
+
+	/* Check whether device enforces the IOMMU correctly */
+
+	/*
+	 * All modern devices must have common and cfg capabilities. We use cfg
+	 * capability for access so that we don't need to worry about resource

drivers/vfio/pci/vfio_pci_virtio.c line 95

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+	 * availability. Slow but sure.
+	 * Note that all vendor-specific fields we access are little-endian
+	 * which matches what pci config accessors expect, so they do byteswap
+	 * for us if appropriate.
+	 */
+	common = virtio_pci_find_capability(dev, VIRTIO_PCI_CAP_COMMON_CFG);
+	cfg = virtio_pci_find_capability(dev, VIRTIO_PCI_CAP_PCI_CFG);
+	if (!cfg || !common) {
+                dev_warn(&dev->dev,
+                         "Virtio device lacks common or pci cfg.\n");  

Whitespace

Quoted 30 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +30 -0
+		return -ENODEV;
+	}
+
+	pci_read_config_byte(dev, common + offsetof(struct virtio_pci_cap,
+						    bar),
+			     &bar);
+	pci_read_config_dword(dev, common + offsetof(struct virtio_pci_cap,
+						    offset),
+			     &offset);
+
+	/* Program cfg capability for dword access into common cfg. */
+	pci_write_config_byte(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  cap.bar),
+			      bar);
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						   cap.length),
+			       0x4);
+
+	/* Select features dword that has VIRTIO_F_IOMMU_PLATFORM. */
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  cap.offset),
+			       offset + offsetof(struct virtio_pci_common_cfg,
+						 device_feature_select));
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  pci_cfg_data),
+			       VIRTIO_F_IOMMU_PLATFORM / 32);
+
+	/* Get the features dword. */
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  cap.offset),

drivers/vfio/pci/vfio_pci_virtio.c line 135

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+			       offset + offsetof(struct virtio_pci_common_cfg,
+						 device_feature));
+	pci_read_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  pci_cfg_data),
+			      &features);
+
+	/* Does this device obey the platform's IOMMU? If not it's an error. */
+	if (!(features & (0x1 << (VIRTIO_F_IOMMU_PLATFORM % 32)))) {
+                dev_warn(&dev->dev,
+                         "Virtio device lacks VIRTIO_F_IOMMU_PLATFORM.\n");  

Whitespace

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +5 -0
+		return -ENODEV;
+	}
+
+	return 0;
+}
drivers/vfio/pci/Makefile +0 -0
index 76d8ec0..e9b20e7 100644

drivers/vfio/pci/Makefile line 6

Quoted 6 lines
drivers/vfio/pci/Makefile +1 -0
@@ -1,5 +1,6 @@
 
 vfio-pci-y := vfio_pci.o vfio_pci_intrs.o vfio_pci_rdwr.o vfio_pci_config.o
+vfio-pci-y += vfio_pci_virtio.o
 vfio-pci-$(CONFIG_VFIO_PCI_IGD) += vfio_pci_igd.o
 
 obj-$(CONFIG_VFIO_PCI) += vfio-pci.o  

Thanks,
Alex

#11 lore raw

Re: [PATCH v2 2/2] vfio: add virtio pci quirk

In reply to Alex Williamson
Quoted 42 lines · On Mon, 29 Aug 2016 21:23:25 -0600 Alex Williamson…
Quoted 30 lines · On Tue, 30 Aug 2016 05:27:17 +0300 "Michael S. Tsirkin" <[email protected]> wrote:

Modern virtio pci devices can set VIRTIO_F_IOMMU_PLATFORM to signal they are safe to use with an IOMMU.

Without this bit, exposing the device to userspace is unsafe, so probe and fail VFIO initialization unless noiommu is enabled.

---
 drivers/vfio/pci/vfio_pci_private.h |   1 +
 drivers/vfio/pci/vfio_pci.c         |  14 ++++
 drivers/vfio/pci/vfio_pci_virtio.c  | 140 ++++++++++++++++++++++++++++++++++++
 drivers/vfio/pci/Makefile           |   1 +
 4 files changed, 156 insertions(+)
 create mode 100644 drivers/vfio/pci/vfio_pci_virtio.c
drivers/vfio/pci/vfio_pci_private.h +1 -0
index 2128de8..2bd5616 100644
@@ -139,4 +139,5 @@ static inline int vfio_pci_igd_init(struct vfio_pci_device *vdev)
 	return -ENODEV;
 }
 #endif
+extern int vfio_pci_virtio_quirk(struct vfio_pci_device *vdev, bool noiommu);
 #endif /* VFIO_PCI_PRIVATE_H */
drivers/vfio/pci/vfio_pci.c +0 -0
index d624a52..e93bf0c 100644

drivers/vfio/pci/vfio_pci.c static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 1239

Quoted 4 lines
drivers/vfio/pci/vfio_pci.c +1 -0
@@ -1236,6 +1236,20 @@ static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 		return ret;
 	}
 
+	if (pdev->vendor == PCI_VENDOR_ID_REDHAT_QUMRANET) {  

Perhaps a vfio_pci_is_virtio() like vga below? Let's test the device ID range initially as well, this test raised a big red flag for me whether all devices within this vendor ID were virtio.

Quoted 8 lines

drivers/vfio/pci/vfio_pci.c static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 1240

Quoted 1 line
drivers/vfio/pci/vfio_pci.c +1 -0
+		bool noiommu = vfio_is_noiommu_group_dev(&pdev->dev);  

I think you can use iommu_present() for this and avoid patch 1of2. noiommu is mutually exclusive to an iommu being present. Seems like all of this logic should be in the quirk itself, I'm not sure what it buys to get the value here but wait until later to use it. Using iommu_present() could also move this test much earlier in vfio_pci_probe() making the exit path easier.

Except then I'm reintroducing the bug fixed by 16ab8a5cbea4 since iommu_present() assumes an IOMMU API based device. I'll try to think if there's another way to avoid adding the is_noiommu function. Thanks,

Alex

Quoted 204 lines
Quoted 22 lines
drivers/vfio/pci/vfio_pci.c +12 -0
+
+		ret = vfio_pci_virtio_quirk(vdev, noiommu);
+		if (ret) {
+			dev_warn(&vdev->pdev->dev,
+				 "Failed to setup Virtio for VFIO\n");
+			vfio_del_group_dev(&pdev->dev);
+			vfio_iommu_group_put(group, &pdev->dev);
+			kfree(vdev);
+			return ret;
+		}
+	}
+
 	if (vfio_pci_is_vga(pdev)) {
 		vga_client_register(pdev, vdev, NULL, vfio_pci_set_vga_decode);
 		vga_set_legacy_decoding(pdev,
drivers/vfio/pci/vfio_pci_virtio.c new file +0 -0
new file mode 100644
index 0000000..e1ecffd

drivers/vfio/pci/vfio_pci_virtio.c line 2

Quoted 2 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +2 -0
@@ -0,0 +1,140 @@
+/*
+ * VFIO PCI Intel Graphics support  
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

drivers/vfio/pci/vfio_pci_virtio.c line 5

Quoted 3 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +3 -0
+ *
+ * Copyright (C) 2016 Red Hat, Inc.  All rights reserved.
+ *	Author: Alex Williamson <[email protected]>  

Update.

drivers/vfio/pci/vfio_pci_virtio.c line 18

Quoted 13 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +13 -0
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * Register a device specific region through which to provide read-only
+ * access to the Intel IGD opregion.  The register defining the opregion
+ * address is also virtualized to prevent user modification.
+ */
+
+#include <linux/io.h>
+#include <linux/pci.h>
+#include <linux/uaccess.h>  

Are io.h and uaccess.h needed?

Quoted 4 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +4 -0
+#include <linux/vfio.h>
+#include <linux/virtio_pci.h>
+#include <linux/virtio_config.h>
+

drivers/vfio/pci/vfio_pci_virtio.c line 32

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+#include "vfio_pci_private.h"
+
+/**
+ * virtio_pci_find_capability - walk capabilities to find device info.
+ * @dev: the pci device
+ * @cfg_type: the VIRTIO_PCI_CAP_* value we seek
+ *
+ * Returns offset of the capability, or 0.
+ */
+static inline int virtio_pci_find_capability(struct pci_dev *dev, u8 cfg_type)  

Does inlining this really make sense?

Quoted 13 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +13 -0
+{
+	int pos;
+
+	for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR);
+	     pos > 0;
+	     pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
+		u8 type;
+		pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
+							 cfg_type),
+				     &type);
+
+		if (type != cfg_type)
+			continue;

drivers/vfio/pci/vfio_pci_virtio.c line 55

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+
+		/* Ignore structures with reserved BAR values */
+		if (type != VIRTIO_PCI_CAP_PCI_CFG) {
+			u8 bar;
+
+			pci_read_config_byte(dev, pos +
+					     offsetof(struct virtio_pci_cap,
+						      bar),
+					     &bar);
+			if (bar > 0x5)  

s/0x5/PCI_STD_RESOURCE_END/

drivers/vfio/pci/vfio_pci_virtio.c line 67

Quoted 12 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +12 -0
+				continue;
+		}
+
+		return pos;
+	}
+	return 0;
+}
+
+
+int vfio_pci_virtio_quirk(struct vfio_pci_device *vdev, bool noiommu)
+{
+	struct pci_dev *dev = vdev->pdev;  

Please use *pdev for consistency with the rest of drivers/vfio/pci/*

Also, is there any reason to pass the vfio_pci_device? There's nothing vfio here otherwise and we could remove more #includes.

drivers/vfio/pci/vfio_pci_virtio.c line 79

Quoted 12 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +12 -0
+	int common, cfg;
+	u32 features;
+	u32 offset;
+	u8 bar;
+
+	/* Without an IOMMU, we don't care */
+	if (noiommu)
+		return 0;
+
+        /* Virtio only owns devices >= 0x1000 and <= 0x107f: leave the rest. */
+        if (dev->device < 0x1000 || dev->device > 0x107f)
+                return 0;  

Whitespace

Quoted 6 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +6 -0
+
+	/* Check whether device enforces the IOMMU correctly */
+
+	/*
+	 * All modern devices must have common and cfg capabilities. We use cfg
+	 * capability for access so that we don't need to worry about resource

drivers/vfio/pci/vfio_pci_virtio.c line 95

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+	 * availability. Slow but sure.
+	 * Note that all vendor-specific fields we access are little-endian
+	 * which matches what pci config accessors expect, so they do byteswap
+	 * for us if appropriate.
+	 */
+	common = virtio_pci_find_capability(dev, VIRTIO_PCI_CAP_COMMON_CFG);
+	cfg = virtio_pci_find_capability(dev, VIRTIO_PCI_CAP_PCI_CFG);
+	if (!cfg || !common) {
+                dev_warn(&dev->dev,
+                         "Virtio device lacks common or pci cfg.\n");  

Whitespace

Quoted 30 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +30 -0
+		return -ENODEV;
+	}
+
+	pci_read_config_byte(dev, common + offsetof(struct virtio_pci_cap,
+						    bar),
+			     &bar);
+	pci_read_config_dword(dev, common + offsetof(struct virtio_pci_cap,
+						    offset),
+			     &offset);
+
+	/* Program cfg capability for dword access into common cfg. */
+	pci_write_config_byte(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  cap.bar),
+			      bar);
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						   cap.length),
+			       0x4);
+
+	/* Select features dword that has VIRTIO_F_IOMMU_PLATFORM. */
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  cap.offset),
+			       offset + offsetof(struct virtio_pci_common_cfg,
+						 device_feature_select));
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  pci_cfg_data),
+			       VIRTIO_F_IOMMU_PLATFORM / 32);
+
+	/* Get the features dword. */
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  cap.offset),

drivers/vfio/pci/vfio_pci_virtio.c line 135

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+			       offset + offsetof(struct virtio_pci_common_cfg,
+						 device_feature));
+	pci_read_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  pci_cfg_data),
+			      &features);
+
+	/* Does this device obey the platform's IOMMU? If not it's an error. */
+	if (!(features & (0x1 << (VIRTIO_F_IOMMU_PLATFORM % 32)))) {
+                dev_warn(&dev->dev,
+                         "Virtio device lacks VIRTIO_F_IOMMU_PLATFORM.\n");  

Whitespace

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +5 -0
+		return -ENODEV;
+	}
+
+	return 0;
+}
drivers/vfio/pci/Makefile +0 -0
index 76d8ec0..e9b20e7 100644

drivers/vfio/pci/Makefile line 6

Quoted 6 lines
drivers/vfio/pci/Makefile +1 -0
@@ -1,5 +1,6 @@
 
 vfio-pci-y := vfio_pci.o vfio_pci_intrs.o vfio_pci_rdwr.o vfio_pci_config.o
+vfio-pci-y += vfio_pci_virtio.o
 vfio-pci-$(CONFIG_VFIO_PCI_IGD) += vfio_pci_igd.o
 
 obj-$(CONFIG_VFIO_PCI) += vfio-pci.o  

Thanks,
Alex

author maintainer
#6 lore raw

Re: [PATCH v2 2/2] vfio: add virtio pci quirk

In reply to Alex Williamson
Quoted 54 lines · Alex Williamson
Quoted 42 lines · On Mon, 29 Aug 2016 21:23:25 -0600 Alex Williamson…
Quoted 30 lines · On Tue, 30 Aug 2016 05:27:17 +0300 "Michael S. Tsirkin" <[email protected]> wrote:

Modern virtio pci devices can set VIRTIO_F_IOMMU_PLATFORM to signal they are safe to use with an IOMMU.

Without this bit, exposing the device to userspace is unsafe, so probe and fail VFIO initialization unless noiommu is enabled.

---
 drivers/vfio/pci/vfio_pci_private.h |   1 +
 drivers/vfio/pci/vfio_pci.c         |  14 ++++
 drivers/vfio/pci/vfio_pci_virtio.c  | 140 ++++++++++++++++++++++++++++++++++++
 drivers/vfio/pci/Makefile           |   1 +
 4 files changed, 156 insertions(+)
 create mode 100644 drivers/vfio/pci/vfio_pci_virtio.c
drivers/vfio/pci/vfio_pci_private.h +1 -0
index 2128de8..2bd5616 100644
@@ -139,4 +139,5 @@ static inline int vfio_pci_igd_init(struct vfio_pci_device *vdev)
 	return -ENODEV;
 }
 #endif
+extern int vfio_pci_virtio_quirk(struct vfio_pci_device *vdev, bool noiommu);
 #endif /* VFIO_PCI_PRIVATE_H */
drivers/vfio/pci/vfio_pci.c +0 -0
index d624a52..e93bf0c 100644

drivers/vfio/pci/vfio_pci.c static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 1239

Quoted 4 lines
drivers/vfio/pci/vfio_pci.c +1 -0
@@ -1236,6 +1236,20 @@ static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 		return ret;
 	}
 
+	if (pdev->vendor == PCI_VENDOR_ID_REDHAT_QUMRANET) {  

Perhaps a vfio_pci_is_virtio() like vga below? Let's test the device ID range initially as well, this test raised a big red flag for me whether all devices within this vendor ID were virtio.

Quoted 8 lines

drivers/vfio/pci/vfio_pci.c static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 1240

Quoted 1 line
drivers/vfio/pci/vfio_pci.c +1 -0
+		bool noiommu = vfio_is_noiommu_group_dev(&pdev->dev);  

I think you can use iommu_present() for this and avoid patch 1of2. noiommu is mutually exclusive to an iommu being present. Seems like all of this logic should be in the quirk itself, I'm not sure what it buys to get the value here but wait until later to use it. Using iommu_present() could also move this test much earlier in vfio_pci_probe() making the exit path easier.

Quoted 5 lines

Except then I'm reintroducing the bug fixed by 16ab8a5cbea4 since iommu_present() assumes an IOMMU API based device. I'll try to think if there's another way to avoid adding the is_noiommu function. Thanks,

Alex

FWIW I'm only too happy if you take over this patch. You need Jason's recent patchset to QEMU to test, but otherwise no special hardware is required.

Quoted 204 lines
Quoted 204 lines
Quoted 22 lines
drivers/vfio/pci/vfio_pci.c +12 -0
+
+		ret = vfio_pci_virtio_quirk(vdev, noiommu);
+		if (ret) {
+			dev_warn(&vdev->pdev->dev,
+				 "Failed to setup Virtio for VFIO\n");
+			vfio_del_group_dev(&pdev->dev);
+			vfio_iommu_group_put(group, &pdev->dev);
+			kfree(vdev);
+			return ret;
+		}
+	}
+
 	if (vfio_pci_is_vga(pdev)) {
 		vga_client_register(pdev, vdev, NULL, vfio_pci_set_vga_decode);
 		vga_set_legacy_decoding(pdev,
drivers/vfio/pci/vfio_pci_virtio.c new file +0 -0
new file mode 100644
index 0000000..e1ecffd

drivers/vfio/pci/vfio_pci_virtio.c line 2

Quoted 2 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +2 -0
@@ -0,0 +1,140 @@
+/*
+ * VFIO PCI Intel Graphics support  
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

drivers/vfio/pci/vfio_pci_virtio.c line 5

Quoted 3 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +3 -0
+ *
+ * Copyright (C) 2016 Red Hat, Inc.  All rights reserved.
+ *	Author: Alex Williamson <[email protected]>  

Update.

drivers/vfio/pci/vfio_pci_virtio.c line 18

Quoted 13 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +13 -0
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * Register a device specific region through which to provide read-only
+ * access to the Intel IGD opregion.  The register defining the opregion
+ * address is also virtualized to prevent user modification.
+ */
+
+#include <linux/io.h>
+#include <linux/pci.h>
+#include <linux/uaccess.h>  

Are io.h and uaccess.h needed?

Quoted 4 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +4 -0
+#include <linux/vfio.h>
+#include <linux/virtio_pci.h>
+#include <linux/virtio_config.h>
+

drivers/vfio/pci/vfio_pci_virtio.c line 32

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+#include "vfio_pci_private.h"
+
+/**
+ * virtio_pci_find_capability - walk capabilities to find device info.
+ * @dev: the pci device
+ * @cfg_type: the VIRTIO_PCI_CAP_* value we seek
+ *
+ * Returns offset of the capability, or 0.
+ */
+static inline int virtio_pci_find_capability(struct pci_dev *dev, u8 cfg_type)  

Does inlining this really make sense?

Quoted 13 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +13 -0
+{
+	int pos;
+
+	for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR);
+	     pos > 0;
+	     pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
+		u8 type;
+		pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
+							 cfg_type),
+				     &type);
+
+		if (type != cfg_type)
+			continue;

drivers/vfio/pci/vfio_pci_virtio.c line 55

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+
+		/* Ignore structures with reserved BAR values */
+		if (type != VIRTIO_PCI_CAP_PCI_CFG) {
+			u8 bar;
+
+			pci_read_config_byte(dev, pos +
+					     offsetof(struct virtio_pci_cap,
+						      bar),
+					     &bar);
+			if (bar > 0x5)  

s/0x5/PCI_STD_RESOURCE_END/

drivers/vfio/pci/vfio_pci_virtio.c line 67

Quoted 12 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +12 -0
+				continue;
+		}
+
+		return pos;
+	}
+	return 0;
+}
+
+
+int vfio_pci_virtio_quirk(struct vfio_pci_device *vdev, bool noiommu)
+{
+	struct pci_dev *dev = vdev->pdev;  

Please use *pdev for consistency with the rest of drivers/vfio/pci/*

Also, is there any reason to pass the vfio_pci_device? There's nothing vfio here otherwise and we could remove more #includes.

drivers/vfio/pci/vfio_pci_virtio.c line 79

Quoted 12 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +12 -0
+	int common, cfg;
+	u32 features;
+	u32 offset;
+	u8 bar;
+
+	/* Without an IOMMU, we don't care */
+	if (noiommu)
+		return 0;
+
+        /* Virtio only owns devices >= 0x1000 and <= 0x107f: leave the rest. */
+        if (dev->device < 0x1000 || dev->device > 0x107f)
+                return 0;  

Whitespace

Quoted 6 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +6 -0
+
+	/* Check whether device enforces the IOMMU correctly */
+
+	/*
+	 * All modern devices must have common and cfg capabilities. We use cfg
+	 * capability for access so that we don't need to worry about resource

drivers/vfio/pci/vfio_pci_virtio.c line 95

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+	 * availability. Slow but sure.
+	 * Note that all vendor-specific fields we access are little-endian
+	 * which matches what pci config accessors expect, so they do byteswap
+	 * for us if appropriate.
+	 */
+	common = virtio_pci_find_capability(dev, VIRTIO_PCI_CAP_COMMON_CFG);
+	cfg = virtio_pci_find_capability(dev, VIRTIO_PCI_CAP_PCI_CFG);
+	if (!cfg || !common) {
+                dev_warn(&dev->dev,
+                         "Virtio device lacks common or pci cfg.\n");  

Whitespace

Quoted 30 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +30 -0
+		return -ENODEV;
+	}
+
+	pci_read_config_byte(dev, common + offsetof(struct virtio_pci_cap,
+						    bar),
+			     &bar);
+	pci_read_config_dword(dev, common + offsetof(struct virtio_pci_cap,
+						    offset),
+			     &offset);
+
+	/* Program cfg capability for dword access into common cfg. */
+	pci_write_config_byte(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  cap.bar),
+			      bar);
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						   cap.length),
+			       0x4);
+
+	/* Select features dword that has VIRTIO_F_IOMMU_PLATFORM. */
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  cap.offset),
+			       offset + offsetof(struct virtio_pci_common_cfg,
+						 device_feature_select));
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  pci_cfg_data),
+			       VIRTIO_F_IOMMU_PLATFORM / 32);
+
+	/* Get the features dword. */
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  cap.offset),

drivers/vfio/pci/vfio_pci_virtio.c line 135

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+			       offset + offsetof(struct virtio_pci_common_cfg,
+						 device_feature));
+	pci_read_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  pci_cfg_data),
+			      &features);
+
+	/* Does this device obey the platform's IOMMU? If not it's an error. */
+	if (!(features & (0x1 << (VIRTIO_F_IOMMU_PLATFORM % 32)))) {
+                dev_warn(&dev->dev,
+                         "Virtio device lacks VIRTIO_F_IOMMU_PLATFORM.\n");  

Whitespace

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +5 -0
+		return -ENODEV;
+	}
+
+	return 0;
+}
drivers/vfio/pci/Makefile +0 -0
index 76d8ec0..e9b20e7 100644

drivers/vfio/pci/Makefile line 6

Quoted 6 lines
drivers/vfio/pci/Makefile +1 -0
@@ -1,5 +1,6 @@
 
 vfio-pci-y := vfio_pci.o vfio_pci_intrs.o vfio_pci_rdwr.o vfio_pci_config.o
+vfio-pci-y += vfio_pci_virtio.o
 vfio-pci-$(CONFIG_VFIO_PCI_IGD) += vfio_pci_igd.o
 
 obj-$(CONFIG_VFIO_PCI) += vfio-pci.o  

Thanks,
Alex

#7 lore raw

Re: [PATCH v2 2/2] vfio: add virtio pci quirk

In reply to Alex Williamson
Quoted 54 lines · On Mon, 29 Aug 2016 21:52:20 -0600 Alex Williamson…
Quoted 42 lines · On Mon, 29 Aug 2016 21:23:25 -0600 Alex Williamson…
Quoted 30 lines · On Tue, 30 Aug 2016 05:27:17 +0300 "Michael S. Tsirkin" <[email protected]> wrote:

Modern virtio pci devices can set VIRTIO_F_IOMMU_PLATFORM to signal they are safe to use with an IOMMU.

Without this bit, exposing the device to userspace is unsafe, so probe and fail VFIO initialization unless noiommu is enabled.

---
 drivers/vfio/pci/vfio_pci_private.h |   1 +
 drivers/vfio/pci/vfio_pci.c         |  14 ++++
 drivers/vfio/pci/vfio_pci_virtio.c  | 140 ++++++++++++++++++++++++++++++++++++
 drivers/vfio/pci/Makefile           |   1 +
 4 files changed, 156 insertions(+)
 create mode 100644 drivers/vfio/pci/vfio_pci_virtio.c
drivers/vfio/pci/vfio_pci_private.h +1 -0
index 2128de8..2bd5616 100644
@@ -139,4 +139,5 @@ static inline int vfio_pci_igd_init(struct vfio_pci_device *vdev)
 	return -ENODEV;
 }
 #endif
+extern int vfio_pci_virtio_quirk(struct vfio_pci_device *vdev, bool noiommu);
 #endif /* VFIO_PCI_PRIVATE_H */
drivers/vfio/pci/vfio_pci.c +0 -0
index d624a52..e93bf0c 100644

drivers/vfio/pci/vfio_pci.c static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 1239

Quoted 4 lines
drivers/vfio/pci/vfio_pci.c +1 -0
@@ -1236,6 +1236,20 @@ static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 		return ret;
 	}
 
+	if (pdev->vendor == PCI_VENDOR_ID_REDHAT_QUMRANET) {    

Perhaps a vfio_pci_is_virtio() like vga below? Let's test the device ID range initially as well, this test raised a big red flag for me whether all devices within this vendor ID were virtio.

Quoted 8 lines

drivers/vfio/pci/vfio_pci.c static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 1240

Quoted 1 line
drivers/vfio/pci/vfio_pci.c +1 -0
+		bool noiommu = vfio_is_noiommu_group_dev(&pdev->dev);    

I think you can use iommu_present() for this and avoid patch 1of2. noiommu is mutually exclusive to an iommu being present. Seems like all of this logic should be in the quirk itself, I'm not sure what it buys to get the value here but wait until later to use it. Using iommu_present() could also move this test much earlier in vfio_pci_probe() making the exit path easier.

Quoted 3 lines

Except then I'm reintroducing the bug fixed by 16ab8a5cbea4 since iommu_present() assumes an IOMMU API based device. I'll try to think if there's another way to avoid adding the is_noiommu function. Thanks,

I think something like this would do it.

drivers/vfio/pci/vfio_pci.c +16 -0
@@ -1214,6 +1214,22 @@ static int vfio_pci_probe(struct pci_dev *pdev, const str
        if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
                return -EINVAL;
 
+       /*
+        * Filter out virtio devices that do not honor the iommu,
+        * but only for real iommu groups.
+        */
+       if (vfio_pci_is_virtio(pdev)) {
+               struct iommu_group *tmp = iommu_group_get(&pdev->dev);
+
+               if (tmp) {
+                       iommu_group_put(tmp);
+
+                       ret = vfio_pci_virtio_quirk(pdev);
+                       if (ret)
+                               return ret;
+               }
+       }
+
        group = vfio_iommu_group_get(&pdev->dev);
        if (!group)
                return -EINVAL;

Thanks,
Alex

Quoted 203 lines
Quoted 203 lines
Quoted 21 lines
drivers/vfio/pci/vfio_pci.c +12 -0
+
+		ret = vfio_pci_virtio_quirk(vdev, noiommu);
+		if (ret) {
+			dev_warn(&vdev->pdev->dev,
+				 "Failed to setup Virtio for VFIO\n");
+			vfio_del_group_dev(&pdev->dev);
+			vfio_iommu_group_put(group, &pdev->dev);
+			kfree(vdev);
+			return ret;
+		}
+	}
+
 	if (vfio_pci_is_vga(pdev)) {
 		vga_client_register(pdev, vdev, NULL, vfio_pci_set_vga_decode);
 		vga_set_legacy_decoding(pdev,
drivers/vfio/pci/vfio_pci_virtio.c new file +0 -0
new file mode 100644
index 0000000..e1ecffd

drivers/vfio/pci/vfio_pci_virtio.c line 2

Quoted 2 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +2 -0
@@ -0,0 +1,140 @@
+/*
+ * VFIO PCI Intel Graphics support    
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  

drivers/vfio/pci/vfio_pci_virtio.c line 5

Quoted 3 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +3 -0
+ *
+ * Copyright (C) 2016 Red Hat, Inc.  All rights reserved.
+ *	Author: Alex Williamson <[email protected]>    

Update.

drivers/vfio/pci/vfio_pci_virtio.c line 18

Quoted 13 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +13 -0
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * Register a device specific region through which to provide read-only
+ * access to the Intel IGD opregion.  The register defining the opregion
+ * address is also virtualized to prevent user modification.
+ */
+
+#include <linux/io.h>
+#include <linux/pci.h>
+#include <linux/uaccess.h>    

Are io.h and uaccess.h needed?

Quoted 4 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +4 -0
+#include <linux/vfio.h>
+#include <linux/virtio_pci.h>
+#include <linux/virtio_config.h>
+

drivers/vfio/pci/vfio_pci_virtio.c line 32

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+#include "vfio_pci_private.h"
+
+/**
+ * virtio_pci_find_capability - walk capabilities to find device info.
+ * @dev: the pci device
+ * @cfg_type: the VIRTIO_PCI_CAP_* value we seek
+ *
+ * Returns offset of the capability, or 0.
+ */
+static inline int virtio_pci_find_capability(struct pci_dev *dev, u8 cfg_type)    

Does inlining this really make sense?

Quoted 13 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +13 -0
+{
+	int pos;
+
+	for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR);
+	     pos > 0;
+	     pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
+		u8 type;
+		pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
+							 cfg_type),
+				     &type);
+
+		if (type != cfg_type)
+			continue;

drivers/vfio/pci/vfio_pci_virtio.c line 55

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+
+		/* Ignore structures with reserved BAR values */
+		if (type != VIRTIO_PCI_CAP_PCI_CFG) {
+			u8 bar;
+
+			pci_read_config_byte(dev, pos +
+					     offsetof(struct virtio_pci_cap,
+						      bar),
+					     &bar);
+			if (bar > 0x5)    

s/0x5/PCI_STD_RESOURCE_END/

drivers/vfio/pci/vfio_pci_virtio.c line 67

Quoted 12 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +12 -0
+				continue;
+		}
+
+		return pos;
+	}
+	return 0;
+}
+
+
+int vfio_pci_virtio_quirk(struct vfio_pci_device *vdev, bool noiommu)
+{
+	struct pci_dev *dev = vdev->pdev;    

Please use *pdev for consistency with the rest of drivers/vfio/pci/*

Also, is there any reason to pass the vfio_pci_device? There's nothing vfio here otherwise and we could remove more #includes.

drivers/vfio/pci/vfio_pci_virtio.c line 79

Quoted 12 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +12 -0
+	int common, cfg;
+	u32 features;
+	u32 offset;
+	u8 bar;
+
+	/* Without an IOMMU, we don't care */
+	if (noiommu)
+		return 0;
+
+        /* Virtio only owns devices >= 0x1000 and <= 0x107f: leave the rest. */
+        if (dev->device < 0x1000 || dev->device > 0x107f)
+                return 0;    

Whitespace

Quoted 6 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +6 -0
+
+	/* Check whether device enforces the IOMMU correctly */
+
+	/*
+	 * All modern devices must have common and cfg capabilities. We use cfg
+	 * capability for access so that we don't need to worry about resource

drivers/vfio/pci/vfio_pci_virtio.c line 95

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+	 * availability. Slow but sure.
+	 * Note that all vendor-specific fields we access are little-endian
+	 * which matches what pci config accessors expect, so they do byteswap
+	 * for us if appropriate.
+	 */
+	common = virtio_pci_find_capability(dev, VIRTIO_PCI_CAP_COMMON_CFG);
+	cfg = virtio_pci_find_capability(dev, VIRTIO_PCI_CAP_PCI_CFG);
+	if (!cfg || !common) {
+                dev_warn(&dev->dev,
+                         "Virtio device lacks common or pci cfg.\n");    

Whitespace

Quoted 30 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +30 -0
+		return -ENODEV;
+	}
+
+	pci_read_config_byte(dev, common + offsetof(struct virtio_pci_cap,
+						    bar),
+			     &bar);
+	pci_read_config_dword(dev, common + offsetof(struct virtio_pci_cap,
+						    offset),
+			     &offset);
+
+	/* Program cfg capability for dword access into common cfg. */
+	pci_write_config_byte(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  cap.bar),
+			      bar);
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						   cap.length),
+			       0x4);
+
+	/* Select features dword that has VIRTIO_F_IOMMU_PLATFORM. */
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  cap.offset),
+			       offset + offsetof(struct virtio_pci_common_cfg,
+						 device_feature_select));
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  pci_cfg_data),
+			       VIRTIO_F_IOMMU_PLATFORM / 32);
+
+	/* Get the features dword. */
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  cap.offset),

drivers/vfio/pci/vfio_pci_virtio.c line 135

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+			       offset + offsetof(struct virtio_pci_common_cfg,
+						 device_feature));
+	pci_read_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  pci_cfg_data),
+			      &features);
+
+	/* Does this device obey the platform's IOMMU? If not it's an error. */
+	if (!(features & (0x1 << (VIRTIO_F_IOMMU_PLATFORM % 32)))) {
+                dev_warn(&dev->dev,
+                         "Virtio device lacks VIRTIO_F_IOMMU_PLATFORM.\n");    

Whitespace

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +5 -0
+		return -ENODEV;
+	}
+
+	return 0;
+}
drivers/vfio/pci/Makefile +0 -0
index 76d8ec0..e9b20e7 100644

drivers/vfio/pci/Makefile line 6

Quoted 6 lines
drivers/vfio/pci/Makefile +1 -0
@@ -1,5 +1,6 @@
 
 vfio-pci-y := vfio_pci.o vfio_pci_intrs.o vfio_pci_rdwr.o vfio_pci_config.o
+vfio-pci-y += vfio_pci_virtio.o
 vfio-pci-$(CONFIG_VFIO_PCI_IGD) += vfio_pci_igd.o
 
 obj-$(CONFIG_VFIO_PCI) += vfio-pci.o    

Thanks,
Alex

author maintainer
#8 lore raw

Re: [PATCH v2 2/2] vfio: add virtio pci quirk

Quoted 89 lines · Alex Williamson
Quoted 54 lines · On Mon, 29 Aug 2016 21:52:20 -0600 Alex Williamson…
Quoted 42 lines · On Mon, 29 Aug 2016 21:23:25 -0600 Alex Williamson…
Quoted 30 lines · On Tue, 30 Aug 2016 05:27:17 +0300 "Michael S. Tsirkin" <[email protected]> wrote:

Modern virtio pci devices can set VIRTIO_F_IOMMU_PLATFORM to signal they are safe to use with an IOMMU.

Without this bit, exposing the device to userspace is unsafe, so probe and fail VFIO initialization unless noiommu is enabled.

---
 drivers/vfio/pci/vfio_pci_private.h |   1 +
 drivers/vfio/pci/vfio_pci.c         |  14 ++++
 drivers/vfio/pci/vfio_pci_virtio.c  | 140 ++++++++++++++++++++++++++++++++++++
 drivers/vfio/pci/Makefile           |   1 +
 4 files changed, 156 insertions(+)
 create mode 100644 drivers/vfio/pci/vfio_pci_virtio.c
drivers/vfio/pci/vfio_pci_private.h +1 -0
index 2128de8..2bd5616 100644
@@ -139,4 +139,5 @@ static inline int vfio_pci_igd_init(struct vfio_pci_device *vdev)
 	return -ENODEV;
 }
 #endif
+extern int vfio_pci_virtio_quirk(struct vfio_pci_device *vdev, bool noiommu);
 #endif /* VFIO_PCI_PRIVATE_H */
drivers/vfio/pci/vfio_pci.c +0 -0
index d624a52..e93bf0c 100644

drivers/vfio/pci/vfio_pci.c static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 1239

Quoted 4 lines
drivers/vfio/pci/vfio_pci.c +1 -0
@@ -1236,6 +1236,20 @@ static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 		return ret;
 	}
 
+	if (pdev->vendor == PCI_VENDOR_ID_REDHAT_QUMRANET) {    

Perhaps a vfio_pci_is_virtio() like vga below? Let's test the device ID range initially as well, this test raised a big red flag for me whether all devices within this vendor ID were virtio.

Quoted 8 lines

drivers/vfio/pci/vfio_pci.c static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 1240

Quoted 1 line
drivers/vfio/pci/vfio_pci.c +1 -0
+		bool noiommu = vfio_is_noiommu_group_dev(&pdev->dev);    

I think you can use iommu_present() for this and avoid patch 1of2. noiommu is mutually exclusive to an iommu being present. Seems like all of this logic should be in the quirk itself, I'm not sure what it buys to get the value here but wait until later to use it. Using iommu_present() could also move this test much earlier in vfio_pci_probe() making the exit path easier.

Quoted 3 lines

Except then I'm reintroducing the bug fixed by 16ab8a5cbea4 since iommu_present() assumes an IOMMU API based device. I'll try to think if there's another way to avoid adding the is_noiommu function. Thanks,

I think something like this would do it.

drivers/vfio/pci/vfio_pci.c +16 -0
@@ -1214,6 +1214,22 @@ static int vfio_pci_probe(struct pci_dev *pdev, const str
        if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
                return -EINVAL;
 
+       /*
+        * Filter out virtio devices that do not honor the iommu,
+        * but only for real iommu groups.
+        */
+       if (vfio_pci_is_virtio(pdev)) {
+               struct iommu_group *tmp = iommu_group_get(&pdev->dev);
+
+               if (tmp) {
+                       iommu_group_put(tmp);
+
+                       ret = vfio_pci_virtio_quirk(pdev);
+                       if (ret)
+                               return ret;
+               }
+       }
+
        group = vfio_iommu_group_get(&pdev->dev);
        if (!group)
                return -EINVAL;
Quoted 2 lines

Thanks,
Alex

Yes but I think this will also prevent binding
a vfio-noiommu to this device.

Arguably this is a separate bug as it's already impossible ... but now that we are disabling regular vfio the noiommu fallback becomes more important.

Any hints on how to fix?

Quoted 203 lines
Quoted 203 lines
Quoted 203 lines
Quoted 21 lines
drivers/vfio/pci/vfio_pci.c +12 -0
+
+		ret = vfio_pci_virtio_quirk(vdev, noiommu);
+		if (ret) {
+			dev_warn(&vdev->pdev->dev,
+				 "Failed to setup Virtio for VFIO\n");
+			vfio_del_group_dev(&pdev->dev);
+			vfio_iommu_group_put(group, &pdev->dev);
+			kfree(vdev);
+			return ret;
+		}
+	}
+
 	if (vfio_pci_is_vga(pdev)) {
 		vga_client_register(pdev, vdev, NULL, vfio_pci_set_vga_decode);
 		vga_set_legacy_decoding(pdev,
drivers/vfio/pci/vfio_pci_virtio.c new file +0 -0
new file mode 100644
index 0000000..e1ecffd

drivers/vfio/pci/vfio_pci_virtio.c line 2

Quoted 2 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +2 -0
@@ -0,0 +1,140 @@
+/*
+ * VFIO PCI Intel Graphics support    
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  

drivers/vfio/pci/vfio_pci_virtio.c line 5

Quoted 3 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +3 -0
+ *
+ * Copyright (C) 2016 Red Hat, Inc.  All rights reserved.
+ *	Author: Alex Williamson <[email protected]>    

Update.

drivers/vfio/pci/vfio_pci_virtio.c line 18

Quoted 13 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +13 -0
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * Register a device specific region through which to provide read-only
+ * access to the Intel IGD opregion.  The register defining the opregion
+ * address is also virtualized to prevent user modification.
+ */
+
+#include <linux/io.h>
+#include <linux/pci.h>
+#include <linux/uaccess.h>    

Are io.h and uaccess.h needed?

Quoted 4 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +4 -0
+#include <linux/vfio.h>
+#include <linux/virtio_pci.h>
+#include <linux/virtio_config.h>
+

drivers/vfio/pci/vfio_pci_virtio.c line 32

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+#include "vfio_pci_private.h"
+
+/**
+ * virtio_pci_find_capability - walk capabilities to find device info.
+ * @dev: the pci device
+ * @cfg_type: the VIRTIO_PCI_CAP_* value we seek
+ *
+ * Returns offset of the capability, or 0.
+ */
+static inline int virtio_pci_find_capability(struct pci_dev *dev, u8 cfg_type)    

Does inlining this really make sense?

Quoted 13 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +13 -0
+{
+	int pos;
+
+	for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR);
+	     pos > 0;
+	     pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
+		u8 type;
+		pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
+							 cfg_type),
+				     &type);
+
+		if (type != cfg_type)
+			continue;

drivers/vfio/pci/vfio_pci_virtio.c line 55

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+
+		/* Ignore structures with reserved BAR values */
+		if (type != VIRTIO_PCI_CAP_PCI_CFG) {
+			u8 bar;
+
+			pci_read_config_byte(dev, pos +
+					     offsetof(struct virtio_pci_cap,
+						      bar),
+					     &bar);
+			if (bar > 0x5)    

s/0x5/PCI_STD_RESOURCE_END/

drivers/vfio/pci/vfio_pci_virtio.c line 67

Quoted 12 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +12 -0
+				continue;
+		}
+
+		return pos;
+	}
+	return 0;
+}
+
+
+int vfio_pci_virtio_quirk(struct vfio_pci_device *vdev, bool noiommu)
+{
+	struct pci_dev *dev = vdev->pdev;    

Please use *pdev for consistency with the rest of drivers/vfio/pci/*

Also, is there any reason to pass the vfio_pci_device? There's nothing vfio here otherwise and we could remove more #includes.

drivers/vfio/pci/vfio_pci_virtio.c line 79

Quoted 12 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +12 -0
+	int common, cfg;
+	u32 features;
+	u32 offset;
+	u8 bar;
+
+	/* Without an IOMMU, we don't care */
+	if (noiommu)
+		return 0;
+
+        /* Virtio only owns devices >= 0x1000 and <= 0x107f: leave the rest. */
+        if (dev->device < 0x1000 || dev->device > 0x107f)
+                return 0;    

Whitespace

Quoted 6 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +6 -0
+
+	/* Check whether device enforces the IOMMU correctly */
+
+	/*
+	 * All modern devices must have common and cfg capabilities. We use cfg
+	 * capability for access so that we don't need to worry about resource

drivers/vfio/pci/vfio_pci_virtio.c line 95

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+	 * availability. Slow but sure.
+	 * Note that all vendor-specific fields we access are little-endian
+	 * which matches what pci config accessors expect, so they do byteswap
+	 * for us if appropriate.
+	 */
+	common = virtio_pci_find_capability(dev, VIRTIO_PCI_CAP_COMMON_CFG);
+	cfg = virtio_pci_find_capability(dev, VIRTIO_PCI_CAP_PCI_CFG);
+	if (!cfg || !common) {
+                dev_warn(&dev->dev,
+                         "Virtio device lacks common or pci cfg.\n");    

Whitespace

Quoted 30 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +30 -0
+		return -ENODEV;
+	}
+
+	pci_read_config_byte(dev, common + offsetof(struct virtio_pci_cap,
+						    bar),
+			     &bar);
+	pci_read_config_dword(dev, common + offsetof(struct virtio_pci_cap,
+						    offset),
+			     &offset);
+
+	/* Program cfg capability for dword access into common cfg. */
+	pci_write_config_byte(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  cap.bar),
+			      bar);
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						   cap.length),
+			       0x4);
+
+	/* Select features dword that has VIRTIO_F_IOMMU_PLATFORM. */
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  cap.offset),
+			       offset + offsetof(struct virtio_pci_common_cfg,
+						 device_feature_select));
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  pci_cfg_data),
+			       VIRTIO_F_IOMMU_PLATFORM / 32);
+
+	/* Get the features dword. */
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  cap.offset),

drivers/vfio/pci/vfio_pci_virtio.c line 135

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+			       offset + offsetof(struct virtio_pci_common_cfg,
+						 device_feature));
+	pci_read_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  pci_cfg_data),
+			      &features);
+
+	/* Does this device obey the platform's IOMMU? If not it's an error. */
+	if (!(features & (0x1 << (VIRTIO_F_IOMMU_PLATFORM % 32)))) {
+                dev_warn(&dev->dev,
+                         "Virtio device lacks VIRTIO_F_IOMMU_PLATFORM.\n");    

Whitespace

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +5 -0
+		return -ENODEV;
+	}
+
+	return 0;
+}
drivers/vfio/pci/Makefile +0 -0
index 76d8ec0..e9b20e7 100644

drivers/vfio/pci/Makefile line 6

Quoted 6 lines
drivers/vfio/pci/Makefile +1 -0
@@ -1,5 +1,6 @@
 
 vfio-pci-y := vfio_pci.o vfio_pci_intrs.o vfio_pci_rdwr.o vfio_pci_config.o
+vfio-pci-y += vfio_pci_virtio.o
 vfio-pci-$(CONFIG_VFIO_PCI_IGD) += vfio_pci_igd.o
 
 obj-$(CONFIG_VFIO_PCI) += vfio-pci.o    

Thanks,
Alex

#12 lore raw

Re: [PATCH v2 2/2] vfio: add virtio pci quirk

In reply to Michael S. Tsirkin
reproduce:

Hi Michael,

[auto build test ERROR on vfio/next]
[also build test ERROR on v4.8-rc4 next-20160825]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
[Suggest to use git(>=2.9.0) format-patch --base=<commit> (or --base=auto for convenience) to record what (public, well-known) commit your patch series was built on] [Check https://git-scm.com/docs/git-format-patch for more information]

url:    https://github.com/0day-ci/linux/commits/Michael-S-Tsirkin/vfio-blacklist-legacy-virtio-devices/20160830-124010
base:   https://github.com/awilliam/linux-vfio.git next
config: x86_64-rhel (attached as .config)
compiler: gcc-6 (Debian 6.1.1-9) 6.1.1 20160705
reproduce:
        # save the attached .config to linux build tree
        make ARCH=x86_64 

All errors (new ones prefixed by >>):

>> ERROR: "vfio_is_noiommu_group_dev" [drivers/vfio/pci/vfio-pci.ko] undefined!

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#9 lore raw

Re: [PATCH v2 2/2] vfio: add virtio pci quirk

In reply to Michael S. Tsirkin
Quoted 96 lines · On Tue, 30 Aug 2016 08:20:38 +0300 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 89 lines · Alex Williamson
Quoted 54 lines · On Mon, 29 Aug 2016 21:52:20 -0600 Alex Williamson…
Quoted 42 lines · On Mon, 29 Aug 2016 21:23:25 -0600 Alex Williamson…
Quoted 30 lines · On Tue, 30 Aug 2016 05:27:17 +0300 "Michael S. Tsirkin" <[email protected]> wrote:

Modern virtio pci devices can set VIRTIO_F_IOMMU_PLATFORM to signal they are safe to use with an IOMMU.

Without this bit, exposing the device to userspace is unsafe, so probe and fail VFIO initialization unless noiommu is enabled.

---
 drivers/vfio/pci/vfio_pci_private.h |   1 +
 drivers/vfio/pci/vfio_pci.c         |  14 ++++
 drivers/vfio/pci/vfio_pci_virtio.c  | 140 ++++++++++++++++++++++++++++++++++++
 drivers/vfio/pci/Makefile           |   1 +
 4 files changed, 156 insertions(+)
 create mode 100644 drivers/vfio/pci/vfio_pci_virtio.c
drivers/vfio/pci/vfio_pci_private.h +1 -0
index 2128de8..2bd5616 100644
@@ -139,4 +139,5 @@ static inline int vfio_pci_igd_init(struct vfio_pci_device *vdev)
 	return -ENODEV;
 }
 #endif
+extern int vfio_pci_virtio_quirk(struct vfio_pci_device *vdev, bool noiommu);
 #endif /* VFIO_PCI_PRIVATE_H */
drivers/vfio/pci/vfio_pci.c +0 -0
index d624a52..e93bf0c 100644

drivers/vfio/pci/vfio_pci.c static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 1239

Quoted 4 lines
drivers/vfio/pci/vfio_pci.c +1 -0
@@ -1236,6 +1236,20 @@ static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 		return ret;
 	}
 
+	if (pdev->vendor == PCI_VENDOR_ID_REDHAT_QUMRANET) {      

Perhaps a vfio_pci_is_virtio() like vga below? Let's test the device ID range initially as well, this test raised a big red flag for me whether all devices within this vendor ID were virtio.

Quoted 8 lines

drivers/vfio/pci/vfio_pci.c static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 1240

Quoted 1 line
drivers/vfio/pci/vfio_pci.c +1 -0
+		bool noiommu = vfio_is_noiommu_group_dev(&pdev->dev);      

I think you can use iommu_present() for this and avoid patch 1of2. noiommu is mutually exclusive to an iommu being present. Seems like all of this logic should be in the quirk itself, I'm not sure what it buys to get the value here but wait until later to use it. Using iommu_present() could also move this test much earlier in vfio_pci_probe() making the exit path easier.

Quoted 3 lines

Except then I'm reintroducing the bug fixed by 16ab8a5cbea4 since iommu_present() assumes an IOMMU API based device. I'll try to think if there's another way to avoid adding the is_noiommu function. Thanks,

I think something like this would do it.

drivers/vfio/pci/vfio_pci.c +16 -0
@@ -1214,6 +1214,22 @@ static int vfio_pci_probe(struct pci_dev *pdev, const str
        if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
                return -EINVAL;
 
+       /*
+        * Filter out virtio devices that do not honor the iommu,
+        * but only for real iommu groups.
+        */
+       if (vfio_pci_is_virtio(pdev)) {
+               struct iommu_group *tmp = iommu_group_get(&pdev->dev);
+
+               if (tmp) {
+                       iommu_group_put(tmp);
+
+                       ret = vfio_pci_virtio_quirk(pdev);
+                       if (ret)
+                               return ret;
+               }
+       }
+
        group = vfio_iommu_group_get(&pdev->dev);
        if (!group)
                return -EINVAL;
Quoted 2 lines

Thanks,
Alex

Yes but I think this will also prevent binding
a vfio-noiommu to this device.

Quoted 5 lines

Arguably this is a separate bug as it's already impossible ... but now that we are disabling regular vfio the noiommu fallback becomes more important.

Any hints on how to fix?

I don't follow, prior to noiommu, we'd simply call iommu_group_get() and rely on the iommu group created by the iommu driver. Either there is one and we allow binding to vfio-pci or there is not one and we would fail. With noiommu, vfio_iommu_group_get() wraps that call in some logic that potentially creates an iommu group if noiommu is enabled. So if we use the real iommu_group_get() prior to that and a group exists, we know we're not using noiommu and we can test the virtio device. If the group doesn't exist here, noiommu plays out the same as it would otherwise (depending on noiommu enabling), a virtio device can be bound regardless of its capabilities and features. I think that's what we want, right? Thanks,

Alex

Quoted 203 lines
Quoted 203 lines
Quoted 203 lines
Quoted 203 lines
Quoted 21 lines
drivers/vfio/pci/vfio_pci.c +12 -0
+
+		ret = vfio_pci_virtio_quirk(vdev, noiommu);
+		if (ret) {
+			dev_warn(&vdev->pdev->dev,
+				 "Failed to setup Virtio for VFIO\n");
+			vfio_del_group_dev(&pdev->dev);
+			vfio_iommu_group_put(group, &pdev->dev);
+			kfree(vdev);
+			return ret;
+		}
+	}
+
 	if (vfio_pci_is_vga(pdev)) {
 		vga_client_register(pdev, vdev, NULL, vfio_pci_set_vga_decode);
 		vga_set_legacy_decoding(pdev,
drivers/vfio/pci/vfio_pci_virtio.c new file +0 -0
new file mode 100644
index 0000000..e1ecffd

drivers/vfio/pci/vfio_pci_virtio.c line 2

Quoted 2 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +2 -0
@@ -0,0 +1,140 @@
+/*
+ * VFIO PCI Intel Graphics support      
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^    

drivers/vfio/pci/vfio_pci_virtio.c line 5

Quoted 3 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +3 -0
+ *
+ * Copyright (C) 2016 Red Hat, Inc.  All rights reserved.
+ *	Author: Alex Williamson <[email protected]>      

Update.

drivers/vfio/pci/vfio_pci_virtio.c line 18

Quoted 13 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +13 -0
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * Register a device specific region through which to provide read-only
+ * access to the Intel IGD opregion.  The register defining the opregion
+ * address is also virtualized to prevent user modification.
+ */
+
+#include <linux/io.h>
+#include <linux/pci.h>
+#include <linux/uaccess.h>      

Are io.h and uaccess.h needed?

Quoted 4 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +4 -0
+#include <linux/vfio.h>
+#include <linux/virtio_pci.h>
+#include <linux/virtio_config.h>
+

drivers/vfio/pci/vfio_pci_virtio.c line 32

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+#include "vfio_pci_private.h"
+
+/**
+ * virtio_pci_find_capability - walk capabilities to find device info.
+ * @dev: the pci device
+ * @cfg_type: the VIRTIO_PCI_CAP_* value we seek
+ *
+ * Returns offset of the capability, or 0.
+ */
+static inline int virtio_pci_find_capability(struct pci_dev *dev, u8 cfg_type)      

Does inlining this really make sense?

Quoted 13 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +13 -0
+{
+	int pos;
+
+	for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR);
+	     pos > 0;
+	     pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
+		u8 type;
+		pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
+							 cfg_type),
+				     &type);
+
+		if (type != cfg_type)
+			continue;

drivers/vfio/pci/vfio_pci_virtio.c line 55

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+
+		/* Ignore structures with reserved BAR values */
+		if (type != VIRTIO_PCI_CAP_PCI_CFG) {
+			u8 bar;
+
+			pci_read_config_byte(dev, pos +
+					     offsetof(struct virtio_pci_cap,
+						      bar),
+					     &bar);
+			if (bar > 0x5)      

s/0x5/PCI_STD_RESOURCE_END/

drivers/vfio/pci/vfio_pci_virtio.c line 67

Quoted 12 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +12 -0
+				continue;
+		}
+
+		return pos;
+	}
+	return 0;
+}
+
+
+int vfio_pci_virtio_quirk(struct vfio_pci_device *vdev, bool noiommu)
+{
+	struct pci_dev *dev = vdev->pdev;      

Please use *pdev for consistency with the rest of drivers/vfio/pci/*

Also, is there any reason to pass the vfio_pci_device? There's nothing vfio here otherwise and we could remove more #includes.

drivers/vfio/pci/vfio_pci_virtio.c line 79

Quoted 12 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +12 -0
+	int common, cfg;
+	u32 features;
+	u32 offset;
+	u8 bar;
+
+	/* Without an IOMMU, we don't care */
+	if (noiommu)
+		return 0;
+
+        /* Virtio only owns devices >= 0x1000 and <= 0x107f: leave the rest. */
+        if (dev->device < 0x1000 || dev->device > 0x107f)
+                return 0;      

Whitespace

Quoted 6 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +6 -0
+
+	/* Check whether device enforces the IOMMU correctly */
+
+	/*
+	 * All modern devices must have common and cfg capabilities. We use cfg
+	 * capability for access so that we don't need to worry about resource

drivers/vfio/pci/vfio_pci_virtio.c line 95

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+	 * availability. Slow but sure.
+	 * Note that all vendor-specific fields we access are little-endian
+	 * which matches what pci config accessors expect, so they do byteswap
+	 * for us if appropriate.
+	 */
+	common = virtio_pci_find_capability(dev, VIRTIO_PCI_CAP_COMMON_CFG);
+	cfg = virtio_pci_find_capability(dev, VIRTIO_PCI_CAP_PCI_CFG);
+	if (!cfg || !common) {
+                dev_warn(&dev->dev,
+                         "Virtio device lacks common or pci cfg.\n");      

Whitespace

Quoted 30 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +30 -0
+		return -ENODEV;
+	}
+
+	pci_read_config_byte(dev, common + offsetof(struct virtio_pci_cap,
+						    bar),
+			     &bar);
+	pci_read_config_dword(dev, common + offsetof(struct virtio_pci_cap,
+						    offset),
+			     &offset);
+
+	/* Program cfg capability for dword access into common cfg. */
+	pci_write_config_byte(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  cap.bar),
+			      bar);
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						   cap.length),
+			       0x4);
+
+	/* Select features dword that has VIRTIO_F_IOMMU_PLATFORM. */
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  cap.offset),
+			       offset + offsetof(struct virtio_pci_common_cfg,
+						 device_feature_select));
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  pci_cfg_data),
+			       VIRTIO_F_IOMMU_PLATFORM / 32);
+
+	/* Get the features dword. */
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  cap.offset),

drivers/vfio/pci/vfio_pci_virtio.c line 135

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+			       offset + offsetof(struct virtio_pci_common_cfg,
+						 device_feature));
+	pci_read_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  pci_cfg_data),
+			      &features);
+
+	/* Does this device obey the platform's IOMMU? If not it's an error. */
+	if (!(features & (0x1 << (VIRTIO_F_IOMMU_PLATFORM % 32)))) {
+                dev_warn(&dev->dev,
+                         "Virtio device lacks VIRTIO_F_IOMMU_PLATFORM.\n");      

Whitespace

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +5 -0
+		return -ENODEV;
+	}
+
+	return 0;
+}
drivers/vfio/pci/Makefile +0 -0
index 76d8ec0..e9b20e7 100644

drivers/vfio/pci/Makefile line 6

Quoted 6 lines
drivers/vfio/pci/Makefile +1 -0
@@ -1,5 +1,6 @@
 
 vfio-pci-y := vfio_pci.o vfio_pci_intrs.o vfio_pci_rdwr.o vfio_pci_config.o
+vfio-pci-y += vfio_pci_virtio.o
 vfio-pci-$(CONFIG_VFIO_PCI_IGD) += vfio_pci_igd.o
 
 obj-$(CONFIG_VFIO_PCI) += vfio-pci.o      

Thanks,
Alex

#10 lore raw

Re: [PATCH v2 2/2] vfio: add virtio pci quirk

In reply to Michael S. Tsirkin
Quoted 96 lines · On Tue, 30 Aug 2016 08:20:38 +0300 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 89 lines · Alex Williamson
Quoted 54 lines · On Mon, 29 Aug 2016 21:52:20 -0600 Alex Williamson…
Quoted 42 lines · On Mon, 29 Aug 2016 21:23:25 -0600 Alex Williamson…
Quoted 30 lines · On Tue, 30 Aug 2016 05:27:17 +0300 "Michael S. Tsirkin" <[email protected]> wrote:

Modern virtio pci devices can set VIRTIO_F_IOMMU_PLATFORM to signal they are safe to use with an IOMMU.

Without this bit, exposing the device to userspace is unsafe, so probe and fail VFIO initialization unless noiommu is enabled.

---
 drivers/vfio/pci/vfio_pci_private.h |   1 +
 drivers/vfio/pci/vfio_pci.c         |  14 ++++
 drivers/vfio/pci/vfio_pci_virtio.c  | 140 ++++++++++++++++++++++++++++++++++++
 drivers/vfio/pci/Makefile           |   1 +
 4 files changed, 156 insertions(+)
 create mode 100644 drivers/vfio/pci/vfio_pci_virtio.c
drivers/vfio/pci/vfio_pci_private.h +1 -0
index 2128de8..2bd5616 100644
@@ -139,4 +139,5 @@ static inline int vfio_pci_igd_init(struct vfio_pci_device *vdev)
 	return -ENODEV;
 }
 #endif
+extern int vfio_pci_virtio_quirk(struct vfio_pci_device *vdev, bool noiommu);
 #endif /* VFIO_PCI_PRIVATE_H */
drivers/vfio/pci/vfio_pci.c +0 -0
index d624a52..e93bf0c 100644

drivers/vfio/pci/vfio_pci.c static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 1239

Quoted 4 lines
drivers/vfio/pci/vfio_pci.c +1 -0
@@ -1236,6 +1236,20 @@ static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 		return ret;
 	}
 
+	if (pdev->vendor == PCI_VENDOR_ID_REDHAT_QUMRANET) {      

Perhaps a vfio_pci_is_virtio() like vga below? Let's test the device ID range initially as well, this test raised a big red flag for me whether all devices within this vendor ID were virtio.

Quoted 8 lines

drivers/vfio/pci/vfio_pci.c static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 1240

Quoted 1 line
drivers/vfio/pci/vfio_pci.c +1 -0
+		bool noiommu = vfio_is_noiommu_group_dev(&pdev->dev);      

I think you can use iommu_present() for this and avoid patch 1of2. noiommu is mutually exclusive to an iommu being present. Seems like all of this logic should be in the quirk itself, I'm not sure what it buys to get the value here but wait until later to use it. Using iommu_present() could also move this test much earlier in vfio_pci_probe() making the exit path easier.

Quoted 3 lines

Except then I'm reintroducing the bug fixed by 16ab8a5cbea4 since iommu_present() assumes an IOMMU API based device. I'll try to think if there's another way to avoid adding the is_noiommu function. Thanks,

I think something like this would do it.

drivers/vfio/pci/vfio_pci.c +16 -0
@@ -1214,6 +1214,22 @@ static int vfio_pci_probe(struct pci_dev *pdev, const str
        if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
                return -EINVAL;
 
+       /*
+        * Filter out virtio devices that do not honor the iommu,
+        * but only for real iommu groups.
+        */
+       if (vfio_pci_is_virtio(pdev)) {
+               struct iommu_group *tmp = iommu_group_get(&pdev->dev);
+
+               if (tmp) {
+                       iommu_group_put(tmp);
+
+                       ret = vfio_pci_virtio_quirk(pdev);
+                       if (ret)
+                               return ret;
+               }
+       }
+
        group = vfio_iommu_group_get(&pdev->dev);
        if (!group)
                return -EINVAL;
Quoted 2 lines

Thanks,
Alex

Yes but I think this will also prevent binding
a vfio-noiommu to this device.

Quoted 5 lines

Arguably this is a separate bug as it's already impossible ... but now that we are disabling regular vfio the noiommu fallback becomes more important.

Any hints on how to fix?

I don't follow, prior to noiommu, we'd simply call iommu_group_get() and rely on the iommu group created by the iommu driver. Either there is one and we allow binding to vfio-pci or there is not one and we would fail. With noiommu, vfio_iommu_group_get() wraps that call in some logic that potentially creates an iommu group if noiommu is enabled. So if we use the real iommu_group_get() prior to that and a group exists, we know we're not using noiommu and we can test the virtio device. If the group doesn't exist here, noiommu plays out the same as it would otherwise (depending on noiommu enabling), a virtio device can be bound regardless of its capabilities and features. I think that's what we want, right? Thanks,

Alex

Quoted 203 lines
Quoted 203 lines
Quoted 203 lines
Quoted 203 lines
Quoted 21 lines
drivers/vfio/pci/vfio_pci.c +12 -0
+
+		ret = vfio_pci_virtio_quirk(vdev, noiommu);
+		if (ret) {
+			dev_warn(&vdev->pdev->dev,
+				 "Failed to setup Virtio for VFIO\n");
+			vfio_del_group_dev(&pdev->dev);
+			vfio_iommu_group_put(group, &pdev->dev);
+			kfree(vdev);
+			return ret;
+		}
+	}
+
 	if (vfio_pci_is_vga(pdev)) {
 		vga_client_register(pdev, vdev, NULL, vfio_pci_set_vga_decode);
 		vga_set_legacy_decoding(pdev,
drivers/vfio/pci/vfio_pci_virtio.c new file +0 -0
new file mode 100644
index 0000000..e1ecffd

drivers/vfio/pci/vfio_pci_virtio.c line 2

Quoted 2 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +2 -0
@@ -0,0 +1,140 @@
+/*
+ * VFIO PCI Intel Graphics support      
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^    

drivers/vfio/pci/vfio_pci_virtio.c line 5

Quoted 3 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +3 -0
+ *
+ * Copyright (C) 2016 Red Hat, Inc.  All rights reserved.
+ *	Author: Alex Williamson <[email protected]>      

Update.

drivers/vfio/pci/vfio_pci_virtio.c line 18

Quoted 13 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +13 -0
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * Register a device specific region through which to provide read-only
+ * access to the Intel IGD opregion.  The register defining the opregion
+ * address is also virtualized to prevent user modification.
+ */
+
+#include <linux/io.h>
+#include <linux/pci.h>
+#include <linux/uaccess.h>      

Are io.h and uaccess.h needed?

Quoted 4 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +4 -0
+#include <linux/vfio.h>
+#include <linux/virtio_pci.h>
+#include <linux/virtio_config.h>
+

drivers/vfio/pci/vfio_pci_virtio.c line 32

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+#include "vfio_pci_private.h"
+
+/**
+ * virtio_pci_find_capability - walk capabilities to find device info.
+ * @dev: the pci device
+ * @cfg_type: the VIRTIO_PCI_CAP_* value we seek
+ *
+ * Returns offset of the capability, or 0.
+ */
+static inline int virtio_pci_find_capability(struct pci_dev *dev, u8 cfg_type)      

Does inlining this really make sense?

Quoted 13 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +13 -0
+{
+	int pos;
+
+	for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR);
+	     pos > 0;
+	     pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
+		u8 type;
+		pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
+							 cfg_type),
+				     &type);
+
+		if (type != cfg_type)
+			continue;

drivers/vfio/pci/vfio_pci_virtio.c line 55

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+
+		/* Ignore structures with reserved BAR values */
+		if (type != VIRTIO_PCI_CAP_PCI_CFG) {
+			u8 bar;
+
+			pci_read_config_byte(dev, pos +
+					     offsetof(struct virtio_pci_cap,
+						      bar),
+					     &bar);
+			if (bar > 0x5)      

s/0x5/PCI_STD_RESOURCE_END/

drivers/vfio/pci/vfio_pci_virtio.c line 67

Quoted 12 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +12 -0
+				continue;
+		}
+
+		return pos;
+	}
+	return 0;
+}
+
+
+int vfio_pci_virtio_quirk(struct vfio_pci_device *vdev, bool noiommu)
+{
+	struct pci_dev *dev = vdev->pdev;      

Please use *pdev for consistency with the rest of drivers/vfio/pci/*

Also, is there any reason to pass the vfio_pci_device? There's nothing vfio here otherwise and we could remove more #includes.

drivers/vfio/pci/vfio_pci_virtio.c line 79

Quoted 12 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +12 -0
+	int common, cfg;
+	u32 features;
+	u32 offset;
+	u8 bar;
+
+	/* Without an IOMMU, we don't care */
+	if (noiommu)
+		return 0;
+
+        /* Virtio only owns devices >= 0x1000 and <= 0x107f: leave the rest. */
+        if (dev->device < 0x1000 || dev->device > 0x107f)
+                return 0;      

Whitespace

Quoted 6 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +6 -0
+
+	/* Check whether device enforces the IOMMU correctly */
+
+	/*
+	 * All modern devices must have common and cfg capabilities. We use cfg
+	 * capability for access so that we don't need to worry about resource

drivers/vfio/pci/vfio_pci_virtio.c line 95

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+	 * availability. Slow but sure.
+	 * Note that all vendor-specific fields we access are little-endian
+	 * which matches what pci config accessors expect, so they do byteswap
+	 * for us if appropriate.
+	 */
+	common = virtio_pci_find_capability(dev, VIRTIO_PCI_CAP_COMMON_CFG);
+	cfg = virtio_pci_find_capability(dev, VIRTIO_PCI_CAP_PCI_CFG);
+	if (!cfg || !common) {
+                dev_warn(&dev->dev,
+                         "Virtio device lacks common or pci cfg.\n");      

Whitespace

Quoted 30 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +30 -0
+		return -ENODEV;
+	}
+
+	pci_read_config_byte(dev, common + offsetof(struct virtio_pci_cap,
+						    bar),
+			     &bar);
+	pci_read_config_dword(dev, common + offsetof(struct virtio_pci_cap,
+						    offset),
+			     &offset);
+
+	/* Program cfg capability for dword access into common cfg. */
+	pci_write_config_byte(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  cap.bar),
+			      bar);
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						   cap.length),
+			       0x4);
+
+	/* Select features dword that has VIRTIO_F_IOMMU_PLATFORM. */
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  cap.offset),
+			       offset + offsetof(struct virtio_pci_common_cfg,
+						 device_feature_select));
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  pci_cfg_data),
+			       VIRTIO_F_IOMMU_PLATFORM / 32);
+
+	/* Get the features dword. */
+	pci_write_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  cap.offset),

drivers/vfio/pci/vfio_pci_virtio.c line 135

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +10 -0
+			       offset + offsetof(struct virtio_pci_common_cfg,
+						 device_feature));
+	pci_read_config_dword(dev, cfg + offsetof(struct virtio_pci_cfg_cap,
+						  pci_cfg_data),
+			      &features);
+
+	/* Does this device obey the platform's IOMMU? If not it's an error. */
+	if (!(features & (0x1 << (VIRTIO_F_IOMMU_PLATFORM % 32)))) {
+                dev_warn(&dev->dev,
+                         "Virtio device lacks VIRTIO_F_IOMMU_PLATFORM.\n");      

Whitespace

Quoted 10 lines
drivers/vfio/pci/vfio_pci_virtio.c new file +5 -0
+		return -ENODEV;
+	}
+
+	return 0;
+}
drivers/vfio/pci/Makefile +0 -0
index 76d8ec0..e9b20e7 100644

drivers/vfio/pci/Makefile line 6

Quoted 6 lines
drivers/vfio/pci/Makefile +1 -0
@@ -1,5 +1,6 @@
 
 vfio-pci-y := vfio_pci.o vfio_pci_intrs.o vfio_pci_rdwr.o vfio_pci_config.o
+vfio-pci-y += vfio_pci_virtio.o
 vfio-pci-$(CONFIG_VFIO_PCI_IGD) += vfio_pci_igd.o
 
 obj-$(CONFIG_VFIO_PCI) += vfio-pci.o      

Thanks,
Alex