[libvirt] [PATCH v3 REBASE 2 07/12] qemu: handle race on device deletion and usb host device plugging

Nikolay Shirokovskiy nshirokovskiy at virtuozzo.com
Tue Oct 29 08:17:58 UTC 2019


Imagine host usb device is unplugged from host and as a result we send
command to qemu to delete appropriate device. Then before qemu device is
deleted host usb device is plugged back. Currently code supposes there is
no remnant device in qemu and will try to add new device and the attempt
will fail.

Instead let's check the device is not yet deleted and postpone adding
qemu device to device_deleted event handler.

Signed-off-by: Nikolay Shirokovskiy <nshirokovskiy at virtuozzo.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413 at gmail.com>
---
 src/qemu/qemu_driver.c | 49 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index fe6b94045b..1a5bf449bf 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -4474,6 +4474,44 @@ processGuestPanicEvent(virQEMUDriverPtr driver,
 }
 
 
+static int
+qemuCheckHostdevPlugged(virQEMUDriverPtr driver,
+                        virDomainObjPtr vm,
+                        const char *devAlias)
+{
+    virDomainHostdevDefPtr hostdev;
+    virDomainHostdevSubsysUSBPtr usbsrc;
+    virDomainDeviceDef dev;
+    int num;
+
+    if (virDomainDefFindDevice(vm->def, devAlias, &dev, false) < 0)
+        return 0;
+
+    if (dev.type != VIR_DOMAIN_DEVICE_HOSTDEV)
+        return 0;
+
+    hostdev = dev.data.hostdev;
+    if (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB)
+        return 0;
+
+    usbsrc = &hostdev->source.subsys.u.usb;
+    if (!usbsrc->vendor || !usbsrc->product)
+        return 0;
+
+    if ((num = virUSBDeviceFindByVendor(usbsrc->vendor, usbsrc->product,
+                                        NULL, false, NULL)) < 0)
+        return -1;
+
+    if (num == 0)
+        return 0;
+
+    if (qemuDomainAttachHostDevice(driver, vm, hostdev) < 0)
+        return -1;
+
+    return 0;
+}
+
+
 static void
 processDeviceDeletedEvent(virQEMUDriverPtr driver,
                           virDomainObjPtr vm,
@@ -4501,6 +4539,11 @@ processDeviceDeletedEvent(virQEMUDriverPtr driver,
 
         if (qemuDomainRemoveDevice(driver, vm, &dev) < 0)
             goto endjob;
+
+        /* Fall thru and save status file even on error condition because
+         * device is removed successfully and changed configuration need
+         * to be saved in status file. */
+        qemuCheckHostdevPlugged(driver, vm, devAlias);
     }
 
     if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm, driver->caps) < 0)
@@ -5098,6 +5141,12 @@ processUSBAddedEvent(virQEMUDriverPtr driver,
     if (i == vm->def->nhostdevs)
         goto cleanup;
 
+    /* if device is not yet even deleted from qemu then handle plugging later.
+     * Or we failed handling host usb device unplugging, then another attempt of
+     * unplug/plug could help. */
+    if (usbsrc->bus || usbsrc->device)
+        goto cleanup;
+
     if (qemuDomainAttachHostDevice(driver, vm, hostdev) < 0)
         goto cleanup;
 
-- 
2.23.0




More information about the libvir-list mailing list