[RFC v1 2/2] vfio/pci: expose only mdev interface if in pci-mdev mode

From: Liu, Yi L
Date: Mon Mar 04 2019 - 08:13:58 EST


If a pci device is wrapped as a mediated device. Then user should
only passthru it via vfio mdev framework. If not, there would be
conflict during the device access. This patch prevents user from
using legacy vfio-pci passthru interface. vfio-pci driver will only
expose mdev interface when working in pci-mdev mode. Thus could
prevent user from passthru device in legacy manner.

Cc: Kevin Tian <kevin.tian@xxxxxxxxx>
Cc: Lu Baolu <baolu.lu@xxxxxxxxxxxxxxx>
Signed-off-by: Liu, Yi L <yi.l.liu@xxxxxxxxx>
---
drivers/vfio/pci/vfio_pci.c | 19 +++++++++++++------
1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
index d1c3fe6..422a3ff 100644
--- a/drivers/vfio/pci/vfio_pci.c
+++ b/drivers/vfio/pci/vfio_pci.c
@@ -1273,11 +1273,15 @@ static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
mutex_init(&vdev->ioeventfds_lock);
INIT_LIST_HEAD(&vdev->ioeventfds_list);

- ret = vfio_add_group_dev(&pdev->dev, &vfio_pci_ops, vdev);
- if (ret) {
- vfio_iommu_group_put(group, &pdev->dev);
- kfree(vdev);
- return ret;
+ if (vfio_pci_mdev_mode) {
+ pci_set_drvdata(pdev, vdev);
+ } else {
+ ret = vfio_add_group_dev(&pdev->dev, &vfio_pci_ops, vdev);
+ if (ret) {
+ vfio_iommu_group_put(group, &pdev->dev);
+ kfree(vdev);
+ return ret;
+ }
}

ret = vfio_pci_reflck_attach(vdev);
@@ -1329,7 +1333,10 @@ static void vfio_pci_remove(struct pci_dev *pdev)
{
struct vfio_pci_device *vdev;

- vdev = vfio_del_group_dev(&pdev->dev);
+ if (vfio_pci_mdev_mode)
+ vdev = pci_get_drvdata(pdev);
+ else
+ vdev = vfio_del_group_dev(&pdev->dev);
if (!vdev)
return;

--
2.7.4