[libvirt] [PATCH v4 03/10] Refuse to reattach from vfio if the iommu group is in use by any domain

Shivaprasad G Bhat sbhat at linux.vnet.ibm.com
Sat Nov 14 08:36:02 UTC 2015


It is incorrect to attempt the device reattach of a function,
when some other domain is using some functions belonging to the same iommu
group.

Signed-off-by: Shivaprasad G Bhat <sbhat at linux.vnet.ibm.com>
---
 src/util/virhostdev.c |   21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/src/util/virhostdev.c b/src/util/virhostdev.c
index de029a0..f24ccd8 100644
--- a/src/util/virhostdev.c
+++ b/src/util/virhostdev.c
@@ -1590,18 +1590,35 @@ virHostdevPCINodeDeviceReAttach(virHostdevManagerPtr hostdev_mgr,
                                 virPCIDevicePtr pci)
 {
     virPCIDeviceAddressPtr devAddr = NULL;
+    bool usesVfio = false;
+    char *drvPath = NULL;
+    char *drvName = NULL;
     struct virHostdevIsPCINodeDeviceUsedData data = {hostdev_mgr, NULL,
                                                      false};
     int ret = -1;
 
+    if (virPCIDeviceGetDriverPathAndName(pci, &drvPath, &drvName) < 0)
+        goto out;
+
+    if (STREQ_NULLABLE(drvName, "vfio-pci"))
+        usesVfio = true;
+
     virObjectLock(hostdev_mgr->activePCIHostdevs);
     virObjectLock(hostdev_mgr->inactivePCIHostdevs);
 
     if (!(devAddr = virPCIDeviceGetAddress(pci)))
         goto out;
 
-    if (virHostdevIsPCINodeDeviceUsed(devAddr, &data))
+    if (usesVfio) {
+    /* Doesn't matter which device. If any domain is actively using the
+     * iommu group, refuse to reattach */
+        if (virPCIDeviceAddressIOMMUGroupIterate(devAddr,
+                                                 virHostdevIsPCINodeDeviceUsed,
+                                                 &data) < 0)
+            goto out;
+    } else if (virHostdevIsPCINodeDeviceUsed(devAddr, &data)) {
         goto out;
+    }
 
     virPCIDeviceReattachInit(pci);
 
@@ -1614,6 +1631,8 @@ virHostdevPCINodeDeviceReAttach(virHostdevManagerPtr hostdev_mgr,
     virObjectUnlock(hostdev_mgr->inactivePCIHostdevs);
     virObjectUnlock(hostdev_mgr->activePCIHostdevs);
     VIR_FREE(devAddr);
+    VIR_FREE(drvPath);
+    VIR_FREE(drvName);
     return ret;
 }
 




More information about the libvir-list mailing list