99cbc7
From 2c396c9f38179ca8a2428d30bc26be1bb05789c1 Mon Sep 17 00:00:00 2001
99cbc7
Message-Id: <2c396c9f38179ca8a2428d30bc26be1bb05789c1@dist-git>
99cbc7
From: Michal Privoznik <mprivozn@redhat.com>
99cbc7
Date: Thu, 11 Apr 2019 15:14:25 -0400
99cbc7
Subject: [PATCH] qemu_hotplug: Introduce and use qemuDomainDeleteDevice
99cbc7
99cbc7
The aim of this function will be to fix return value of
99cbc7
qemuMonitorDelDevice() in one specific case. But that is yet to
99cbc7
come. Right now this is nothing but a plain substitution.
99cbc7
99cbc7
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
99cbc7
ACKed-by: Peter Krempa <pkrempa@redhat.com>
99cbc7
(cherry picked from commit 4cd13478ac331f5e42c926d4f2111dd89d2970a6)
99cbc7
99cbc7
Partially-Resolves: https://bugzilla.redhat.com/1658198
99cbc7
Signed-off-by: Laine Stump <laine@redhat.com>
99cbc7
99cbc7
Conflicts: src/qemu/qemu_hotplug.c:
99cbc7
   - qemuDomainDetachExtensionDevice() (erroneously) added to
99cbc7
     qemuDomainDetachControllerDevice upstream, doesn't exist
99cbc7
     downstream.
99cbc7
Signed-off-by: Laine Stump <laine@laine.org>
99cbc7
Message-Id: <20190411191453.24055-14-laine@redhat.com>
99cbc7
Acked-by: Michal Privoznik <mprivozn@redhat.com>
99cbc7
---
99cbc7
 src/qemu/qemu_hotplug.c | 239 ++++++++++++++--------------------------
99cbc7
 1 file changed, 82 insertions(+), 157 deletions(-)
99cbc7
99cbc7
diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
99cbc7
index 963b87f798..1f41efa180 100644
99cbc7
--- a/src/qemu/qemu_hotplug.c
99cbc7
+++ b/src/qemu/qemu_hotplug.c
99cbc7
@@ -68,6 +68,45 @@ VIR_LOG_INIT("qemu.qemu_hotplug");
99cbc7
 unsigned long long qemuDomainRemoveDeviceWaitTime = 1000ull * 5;
99cbc7
 
99cbc7
 
99cbc7
+/**
99cbc7
+ * qemuDomainDeleteDevice:
99cbc7
+ * @vm: domain object
99cbc7
+ * @alias: device to remove
99cbc7
+ *
99cbc7
+ * This is a wrapper over qemuMonitorDelDevice() plus enter/exit
99cbc7
+ * monitor calls.  This function MUST be used instead of plain
99cbc7
+ * qemuMonitorDelDevice() in all places where @alias represents a
99cbc7
+ * device from domain XML, i.e. caller marks the device for
99cbc7
+ * removal and then calls qemuDomainWaitForDeviceRemoval()
99cbc7
+ * followed by qemuDomainRemove*Device().
99cbc7
+ *
99cbc7
+ * For collateral devices (e.g. extension devices like zPCI) it
99cbc7
+ * is safe to use plain qemuMonitorDelDevice().
99cbc7
+ *
99cbc7
+ * Upon entry, @vm must be locked.
99cbc7
+ *
99cbc7
+ * Returns: 0 on success,
99cbc7
+ *         -1 otherwise.
99cbc7
+ */
99cbc7
+static int
99cbc7
+qemuDomainDeleteDevice(virDomainObjPtr vm,
99cbc7
+                       const char *alias)
99cbc7
+{
99cbc7
+    qemuDomainObjPrivatePtr priv = vm->privateData;
99cbc7
+    virQEMUDriverPtr driver = priv->driver;
99cbc7
+    int rc;
99cbc7
+
99cbc7
+    qemuDomainObjEnterMonitor(driver, vm);
99cbc7
+
99cbc7
+    rc = qemuMonitorDelDevice(priv->mon, alias);
99cbc7
+
99cbc7
+    if (qemuDomainObjExitMonitor(driver, vm) < 0)
99cbc7
+        rc = -1;
99cbc7
+
99cbc7
+    return rc;
99cbc7
+}
99cbc7
+
99cbc7
+
99cbc7
 /**
99cbc7
  * qemuHotplugPrepareDiskAccess:
99cbc7
  * @driver: qemu driver struct
99cbc7
@@ -4689,7 +4728,6 @@ qemuDomainDetachVirtioDiskDevice(virQEMUDriverPtr driver,
99cbc7
                                  bool async)
99cbc7
 {
99cbc7
     int ret = -1;
99cbc7
-    qemuDomainObjPrivatePtr priv = vm->privateData;
99cbc7
 
99cbc7
     if (qemuIsMultiFunctionDevice(vm->def, &detach->info)) {
99cbc7
         virReportError(VIR_ERR_OPERATION_FAILED,
99cbc7
@@ -4701,15 +4739,11 @@ qemuDomainDetachVirtioDiskDevice(virQEMUDriverPtr driver,
99cbc7
     if (!async)
99cbc7
         qemuDomainMarkDeviceForRemoval(vm, &detach->info);
99cbc7
 
99cbc7
-    qemuDomainObjEnterMonitor(driver, vm);
99cbc7
-    if (qemuMonitorDelDevice(priv->mon, detach->info.alias) < 0) {
99cbc7
-        if (qemuDomainObjExitMonitor(driver, vm) < 0)
99cbc7
-            goto cleanup;
99cbc7
-        virDomainAuditDisk(vm, detach->src, NULL, "detach", false);
99cbc7
+    if (qemuDomainDeleteDevice(vm, detach->info.alias) < 0) {
99cbc7
+        if (virDomainObjIsActive(vm))
99cbc7
+            virDomainAuditDisk(vm, detach->src, NULL, "detach", false);
99cbc7
         goto cleanup;
99cbc7
     }
99cbc7
-    if (qemuDomainObjExitMonitor(driver, vm) < 0)
99cbc7
-        goto cleanup;
99cbc7
 
99cbc7
     if (async) {
99cbc7
         ret = 0;
99cbc7
@@ -4731,7 +4765,6 @@ qemuDomainDetachDiskDevice(virQEMUDriverPtr driver,
99cbc7
                            bool async)
99cbc7
 {
99cbc7
     int ret = -1;
99cbc7
-    qemuDomainObjPrivatePtr priv = vm->privateData;
99cbc7
 
99cbc7
     if (qemuDomainDiskBlockJobIsActive(detach))
99cbc7
         goto cleanup;
99cbc7
@@ -4739,15 +4772,11 @@ qemuDomainDetachDiskDevice(virQEMUDriverPtr driver,
99cbc7
     if (!async)
99cbc7
         qemuDomainMarkDeviceForRemoval(vm, &detach->info);
99cbc7
 
99cbc7
-    qemuDomainObjEnterMonitor(driver, vm);
99cbc7
-    if (qemuMonitorDelDevice(priv->mon, detach->info.alias) < 0) {
99cbc7
-        if (qemuDomainObjExitMonitor(driver, vm) < 0)
99cbc7
-            goto cleanup;
99cbc7
-        virDomainAuditDisk(vm, detach->src, NULL, "detach", false);
99cbc7
+    if (qemuDomainDeleteDevice(vm, detach->info.alias) < 0) {
99cbc7
+        if (virDomainObjIsActive(vm))
99cbc7
+            virDomainAuditDisk(vm, detach->src, NULL, "detach", false);
99cbc7
         goto cleanup;
99cbc7
     }
99cbc7
-    if (qemuDomainObjExitMonitor(driver, vm) < 0)
99cbc7
-        goto cleanup;
99cbc7
 
99cbc7
     if (async) {
99cbc7
         ret = 0;
99cbc7
@@ -4882,7 +4911,6 @@ int qemuDomainDetachControllerDevice(virQEMUDriverPtr driver,
99cbc7
 {
99cbc7
     int idx, ret = -1;
99cbc7
     virDomainControllerDefPtr detach = NULL;
99cbc7
-    qemuDomainObjPrivatePtr priv = vm->privateData;
99cbc7
 
99cbc7
     if ((idx = virDomainControllerFind(vm->def,
99cbc7
                                        dev->data.controller->type,
99cbc7
@@ -4928,12 +4956,7 @@ int qemuDomainDetachControllerDevice(virQEMUDriverPtr driver,
99cbc7
     if (!async)
99cbc7
         qemuDomainMarkDeviceForRemoval(vm, &detach->info);
99cbc7
 
99cbc7
-    qemuDomainObjEnterMonitor(driver, vm);
99cbc7
-    if (qemuMonitorDelDevice(priv->mon, detach->info.alias) < 0) {
99cbc7
-        ignore_value(qemuDomainObjExitMonitor(driver, vm));
99cbc7
-        goto cleanup;
99cbc7
-    }
99cbc7
-    if (qemuDomainObjExitMonitor(driver, vm) < 0)
99cbc7
+    if (qemuDomainDeleteDevice(vm, detach->info.alias) < 0)
99cbc7
         goto cleanup;
99cbc7
 
99cbc7
     if (async) {
99cbc7
@@ -4950,14 +4973,11 @@ int qemuDomainDetachControllerDevice(virQEMUDriverPtr driver,
99cbc7
 }
99cbc7
 
99cbc7
 static int
99cbc7
-qemuDomainDetachHostPCIDevice(virQEMUDriverPtr driver,
99cbc7
-                              virDomainObjPtr vm,
99cbc7
+qemuDomainDetachHostPCIDevice(virDomainObjPtr vm,
99cbc7
                               virDomainHostdevDefPtr detach,
99cbc7
                               bool async)
99cbc7
 {
99cbc7
-    qemuDomainObjPrivatePtr priv = vm->privateData;
99cbc7
     virDomainHostdevSubsysPCIPtr pcisrc = &detach->source.subsys.u.pci;
99cbc7
-    int ret;
99cbc7
 
99cbc7
     if (qemuIsMultiFunctionDevice(vm->def, detach->info)) {
99cbc7
         virReportError(VIR_ERR_OPERATION_FAILED,
99cbc7
@@ -4970,23 +4990,14 @@ qemuDomainDetachHostPCIDevice(virQEMUDriverPtr driver,
99cbc7
     if (!async)
99cbc7
         qemuDomainMarkDeviceForRemoval(vm, detach->info);
99cbc7
 
99cbc7
-    qemuDomainObjEnterMonitor(driver, vm);
99cbc7
-    ret = qemuMonitorDelDevice(priv->mon, detach->info->alias);
99cbc7
-    if (qemuDomainObjExitMonitor(driver, vm) < 0)
99cbc7
-        ret = -1;
99cbc7
-
99cbc7
-    return ret;
99cbc7
+    return qemuDomainDeleteDevice(vm, detach->info->alias);
99cbc7
 }
99cbc7
 
99cbc7
 static int
99cbc7
-qemuDomainDetachHostUSBDevice(virQEMUDriverPtr driver,
99cbc7
-                              virDomainObjPtr vm,
99cbc7
+qemuDomainDetachHostUSBDevice(virDomainObjPtr vm,
99cbc7
                               virDomainHostdevDefPtr detach,
99cbc7
                               bool async)
99cbc7
 {
99cbc7
-    qemuDomainObjPrivatePtr priv = vm->privateData;
99cbc7
-    int ret;
99cbc7
-
99cbc7
     if (!detach->info->alias) {
99cbc7
         virReportError(VIR_ERR_OPERATION_FAILED,
99cbc7
                        "%s", _("device cannot be detached without a device alias"));
99cbc7
@@ -4996,23 +5007,14 @@ qemuDomainDetachHostUSBDevice(virQEMUDriverPtr driver,
99cbc7
     if (!async)
99cbc7
         qemuDomainMarkDeviceForRemoval(vm, detach->info);
99cbc7
 
99cbc7
-    qemuDomainObjEnterMonitor(driver, vm);
99cbc7
-    ret = qemuMonitorDelDevice(priv->mon, detach->info->alias);
99cbc7
-    if (qemuDomainObjExitMonitor(driver, vm) < 0)
99cbc7
-        ret = -1;
99cbc7
-
99cbc7
-    return ret;
99cbc7
+    return qemuDomainDeleteDevice(vm, detach->info->alias);
99cbc7
 }
99cbc7
 
99cbc7
 static int
99cbc7
-qemuDomainDetachHostSCSIDevice(virQEMUDriverPtr driver,
99cbc7
-                               virDomainObjPtr vm,
99cbc7
+qemuDomainDetachHostSCSIDevice(virDomainObjPtr vm,
99cbc7
                                virDomainHostdevDefPtr detach,
99cbc7
                                bool async)
99cbc7
 {
99cbc7
-    qemuDomainObjPrivatePtr priv = vm->privateData;
99cbc7
-    int ret = -1;
99cbc7
-
99cbc7
     if (!detach->info->alias) {
99cbc7
         virReportError(VIR_ERR_OPERATION_FAILED,
99cbc7
                        "%s", _("device cannot be detached without a device alias"));
99cbc7
@@ -5022,24 +5024,14 @@ qemuDomainDetachHostSCSIDevice(virQEMUDriverPtr driver,
99cbc7
     if (!async)
99cbc7
         qemuDomainMarkDeviceForRemoval(vm, detach->info);
99cbc7
 
99cbc7
-    qemuDomainObjEnterMonitor(driver, vm);
99cbc7
-    ret = qemuMonitorDelDevice(priv->mon, detach->info->alias);
99cbc7
-
99cbc7
-    if (qemuDomainObjExitMonitor(driver, vm) < 0)
99cbc7
-        return -1;
99cbc7
-
99cbc7
-    return ret;
99cbc7
+    return qemuDomainDeleteDevice(vm, detach->info->alias);
99cbc7
 }
99cbc7
 
99cbc7
 static int
99cbc7
-qemuDomainDetachSCSIVHostDevice(virQEMUDriverPtr driver,
99cbc7
-                                virDomainObjPtr vm,
99cbc7
+qemuDomainDetachSCSIVHostDevice(virDomainObjPtr vm,
99cbc7
                                 virDomainHostdevDefPtr detach,
99cbc7
                                 bool async)
99cbc7
 {
99cbc7
-    qemuDomainObjPrivatePtr priv = vm->privateData;
99cbc7
-    int ret = -1;
99cbc7
-
99cbc7
     if (!detach->info->alias) {
99cbc7
         virReportError(VIR_ERR_OPERATION_FAILED,
99cbc7
                        "%s", _("device cannot be detached without a device alias"));
99cbc7
@@ -5049,25 +5041,15 @@ qemuDomainDetachSCSIVHostDevice(virQEMUDriverPtr driver,
99cbc7
     if (!async)
99cbc7
         qemuDomainMarkDeviceForRemoval(vm, detach->info);
99cbc7
 
99cbc7
-    qemuDomainObjEnterMonitor(driver, vm);
99cbc7
-    ret = qemuMonitorDelDevice(priv->mon, detach->info->alias);
99cbc7
-
99cbc7
-    if (qemuDomainObjExitMonitor(driver, vm) < 0)
99cbc7
-        return -1;
99cbc7
-
99cbc7
-    return ret;
99cbc7
+    return qemuDomainDeleteDevice(vm, detach->info->alias);
99cbc7
 }
99cbc7
 
99cbc7
 
99cbc7
 static int
99cbc7
-qemuDomainDetachMediatedDevice(virQEMUDriverPtr driver,
99cbc7
-                               virDomainObjPtr vm,
99cbc7
+qemuDomainDetachMediatedDevice(virDomainObjPtr vm,
99cbc7
                                virDomainHostdevDefPtr detach,
99cbc7
                                bool async)
99cbc7
 {
99cbc7
-    int ret = -1;
99cbc7
-    qemuDomainObjPrivatePtr priv = vm->privateData;
99cbc7
-
99cbc7
     if (!detach->info->alias) {
99cbc7
         virReportError(VIR_ERR_OPERATION_FAILED, "%s",
99cbc7
                        _("device cannot be detached without a device alias"));
99cbc7
@@ -5077,12 +5059,7 @@ qemuDomainDetachMediatedDevice(virQEMUDriverPtr driver,
99cbc7
     if (!async)
99cbc7
         qemuDomainMarkDeviceForRemoval(vm, detach->info);
99cbc7
 
99cbc7
-    qemuDomainObjEnterMonitor(driver, vm);
99cbc7
-    ret = qemuMonitorDelDevice(priv->mon, detach->info->alias);
99cbc7
-    if (qemuDomainObjExitMonitor(driver, vm) < 0)
99cbc7
-        ret = -1;
99cbc7
-
99cbc7
-    return ret;
99cbc7
+    return qemuDomainDeleteDevice(vm, detach->info->alias);
99cbc7
 }
99cbc7
 
99cbc7
 
99cbc7
@@ -5099,19 +5076,19 @@ qemuDomainDetachThisHostDevice(virQEMUDriverPtr driver,
99cbc7
 
99cbc7
     switch (detach->source.subsys.type) {
99cbc7
     case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI:
99cbc7
-        ret = qemuDomainDetachHostPCIDevice(driver, vm, detach, async);
99cbc7
+        ret = qemuDomainDetachHostPCIDevice(vm, detach, async);
99cbc7
         break;
99cbc7
     case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB:
99cbc7
-        ret = qemuDomainDetachHostUSBDevice(driver, vm, detach, async);
99cbc7
+        ret = qemuDomainDetachHostUSBDevice(vm, detach, async);
99cbc7
         break;
99cbc7
     case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI:
99cbc7
-        ret = qemuDomainDetachHostSCSIDevice(driver, vm, detach, async);
99cbc7
+        ret = qemuDomainDetachHostSCSIDevice(vm, detach, async);
99cbc7
         break;
99cbc7
     case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI_HOST:
99cbc7
-        ret = qemuDomainDetachSCSIVHostDevice(driver, vm, detach, async);
99cbc7
+        ret = qemuDomainDetachSCSIVHostDevice(vm, detach, async);
99cbc7
         break;
99cbc7
     case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV:
99cbc7
-        ret = qemuDomainDetachMediatedDevice(driver, vm, detach, async);
99cbc7
+        ret = qemuDomainDetachMediatedDevice(vm, detach, async);
99cbc7
         break;
99cbc7
     default:
99cbc7
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
99cbc7
@@ -5228,7 +5205,6 @@ qemuDomainDetachShmemDevice(virQEMUDriverPtr driver,
99cbc7
     int ret = -1;
99cbc7
     ssize_t idx = -1;
99cbc7
     virDomainShmemDefPtr shmem = NULL;
99cbc7
-    qemuDomainObjPrivatePtr priv = vm->privateData;
99cbc7
 
99cbc7
     if ((idx = virDomainShmemDefFind(vm->def, dev)) < 0) {
99cbc7
         virReportError(VIR_ERR_DEVICE_MISSING,
99cbc7
@@ -5257,12 +5233,7 @@ qemuDomainDetachShmemDevice(virQEMUDriverPtr driver,
99cbc7
     if (!async)
99cbc7
         qemuDomainMarkDeviceForRemoval(vm, &shmem->info);
99cbc7
 
99cbc7
-    qemuDomainObjEnterMonitor(driver, vm);
99cbc7
-    if (qemuMonitorDelDevice(priv->mon, shmem->info.alias) < 0) {
99cbc7
-        ignore_value(qemuDomainObjExitMonitor(driver, vm));
99cbc7
-        goto cleanup;
99cbc7
-    }
99cbc7
-    if (qemuDomainObjExitMonitor(driver, vm) < 0)
99cbc7
+    if (qemuDomainDeleteDevice(vm, shmem->info.alias) < 0)
99cbc7
         goto cleanup;
99cbc7
 
99cbc7
     if (async) {
99cbc7
@@ -5287,7 +5258,6 @@ qemuDomainDetachWatchdog(virQEMUDriverPtr driver,
99cbc7
 {
99cbc7
     int ret = -1;
99cbc7
     virDomainWatchdogDefPtr watchdog = vm->def->watchdog;
99cbc7
-    qemuDomainObjPrivatePtr priv = vm->privateData;
99cbc7
 
99cbc7
     if (!watchdog) {
99cbc7
         virReportError(VIR_ERR_DEVICE_MISSING, "%s",
99cbc7
@@ -5318,12 +5288,7 @@ qemuDomainDetachWatchdog(virQEMUDriverPtr driver,
99cbc7
     if (!async)
99cbc7
         qemuDomainMarkDeviceForRemoval(vm, &watchdog->info);
99cbc7
 
99cbc7
-    qemuDomainObjEnterMonitor(driver, vm);
99cbc7
-    if (qemuMonitorDelDevice(priv->mon, watchdog->info.alias) < 0) {
99cbc7
-        ignore_value(qemuDomainObjExitMonitor(driver, vm));
99cbc7
-        goto cleanup;
99cbc7
-    }
99cbc7
-    if (qemuDomainObjExitMonitor(driver, vm) < 0)
99cbc7
+    if (qemuDomainDeleteDevice(vm, watchdog->info.alias) < 0)
99cbc7
         goto cleanup;
99cbc7
 
99cbc7
     if (async) {
99cbc7
@@ -5347,7 +5312,6 @@ qemuDomainDetachRedirdevDevice(virQEMUDriverPtr driver,
99cbc7
                                bool async)
99cbc7
 {
99cbc7
     int ret = -1;
99cbc7
-    qemuDomainObjPrivatePtr priv = vm->privateData;
99cbc7
     virDomainRedirdevDefPtr tmpRedirdevDef;
99cbc7
     ssize_t idx;
99cbc7
 
99cbc7
@@ -5368,12 +5332,7 @@ qemuDomainDetachRedirdevDevice(virQEMUDriverPtr driver,
99cbc7
     if (!async)
99cbc7
         qemuDomainMarkDeviceForRemoval(vm, &tmpRedirdevDef->info);
99cbc7
 
99cbc7
-    qemuDomainObjEnterMonitor(driver, vm);
99cbc7
-    if (qemuMonitorDelDevice(priv->mon, tmpRedirdevDef->info.alias) < 0) {
99cbc7
-        ignore_value(qemuDomainObjExitMonitor(driver, vm));
99cbc7
-        goto cleanup;
99cbc7
-    }
99cbc7
-    if (qemuDomainObjExitMonitor(driver, vm) < 0)
99cbc7
+    if (qemuDomainDeleteDevice(vm, tmpRedirdevDef->info.alias) < 0)
99cbc7
         goto cleanup;
99cbc7
 
99cbc7
     if (async) {
99cbc7
@@ -5398,7 +5357,6 @@ qemuDomainDetachNetDevice(virQEMUDriverPtr driver,
99cbc7
 {
99cbc7
     int detachidx, ret = -1;
99cbc7
     virDomainNetDefPtr detach = NULL;
99cbc7
-    qemuDomainObjPrivatePtr priv = vm->privateData;
99cbc7
 
99cbc7
     if ((detachidx = virDomainNetFindIdx(vm->def, dev->data.net)) < 0)
99cbc7
         goto cleanup;
99cbc7
@@ -5439,15 +5397,11 @@ qemuDomainDetachNetDevice(virQEMUDriverPtr driver,
99cbc7
     if (!async)
99cbc7
         qemuDomainMarkDeviceForRemoval(vm, &detach->info);
99cbc7
 
99cbc7
-    qemuDomainObjEnterMonitor(driver, vm);
99cbc7
-    if (qemuMonitorDelDevice(priv->mon, detach->info.alias) < 0) {
99cbc7
-        if (qemuDomainObjExitMonitor(driver, vm) < 0)
99cbc7
-            goto cleanup;
99cbc7
-        virDomainAuditNet(vm, detach, NULL, "detach", false);
99cbc7
+    if (qemuDomainDeleteDevice(vm, detach->info.alias) < 0) {
99cbc7
+        if (virDomainObjIsActive(vm))
99cbc7
+            virDomainAuditNet(vm, detach, NULL, "detach", false);
99cbc7
         goto cleanup;
99cbc7
     }
99cbc7
-    if (qemuDomainObjExitMonitor(driver, vm) < 0)
99cbc7
-        goto cleanup;
99cbc7
 
99cbc7
     if (async) {
99cbc7
         ret = 0;
99cbc7
@@ -5610,20 +5564,19 @@ int qemuDomainDetachChrDevice(virQEMUDriverPtr driver,
99cbc7
     if (!async && !guestfwd)
99cbc7
         qemuDomainMarkDeviceForRemoval(vm, &tmpChr->info);
99cbc7
 
99cbc7
-    qemuDomainObjEnterMonitor(driver, vm);
99cbc7
     if (guestfwd) {
99cbc7
-        if (qemuMonitorRemoveNetdev(priv->mon, tmpChr->info.alias) < 0) {
99cbc7
-            ignore_value(qemuDomainObjExitMonitor(driver, vm));
99cbc7
+        int rc;
99cbc7
+        qemuDomainObjEnterMonitor(driver, vm);
99cbc7
+        rc = qemuMonitorRemoveNetdev(priv->mon, tmpChr->info.alias);
99cbc7
+        if (qemuDomainObjExitMonitor(driver, vm) < 0)
99cbc7
+            rc = -1;
99cbc7
+
99cbc7
+        if (rc < 0)
99cbc7
             goto cleanup;
99cbc7
-        }
99cbc7
     } else {
99cbc7
-        if (qemuMonitorDelDevice(priv->mon, tmpChr->info.alias) < 0) {
99cbc7
-            ignore_value(qemuDomainObjExitMonitor(driver, vm));
99cbc7
+        if (qemuDomainDeleteDevice(vm, tmpChr->info.alias) < 0)
99cbc7
             goto cleanup;
99cbc7
-        }
99cbc7
     }
99cbc7
-    if (qemuDomainObjExitMonitor(driver, vm) < 0)
99cbc7
-        goto cleanup;
99cbc7
 
99cbc7
     if (guestfwd) {
99cbc7
         ret = qemuDomainRemoveChrDevice(driver, vm, tmpChr, false);
99cbc7
@@ -5647,10 +5600,8 @@ qemuDomainDetachRNGDevice(virQEMUDriverPtr driver,
99cbc7
                           virDomainRNGDefPtr rng,
99cbc7
                           bool async)
99cbc7
 {
99cbc7
-    qemuDomainObjPrivatePtr priv = vm->privateData;
99cbc7
     ssize_t idx;
99cbc7
     virDomainRNGDefPtr tmpRNG;
99cbc7
-    int rc;
99cbc7
     int ret = -1;
99cbc7
 
99cbc7
     if ((idx = virDomainRNGFind(vm->def, rng)) < 0) {
99cbc7
@@ -5672,9 +5623,7 @@ qemuDomainDetachRNGDevice(virQEMUDriverPtr driver,
99cbc7
     if (!async)
99cbc7
         qemuDomainMarkDeviceForRemoval(vm, &tmpRNG->info);
99cbc7
 
99cbc7
-    qemuDomainObjEnterMonitor(driver, vm);
99cbc7
-    rc = qemuMonitorDelDevice(priv->mon, tmpRNG->info.alias);
99cbc7
-    if (qemuDomainObjExitMonitor(driver, vm) || rc < 0)
99cbc7
+    if (qemuDomainDeleteDevice(vm, tmpRNG->info.alias) < 0)
99cbc7
         goto cleanup;
99cbc7
 
99cbc7
     if (async) {
99cbc7
@@ -5697,10 +5646,8 @@ qemuDomainDetachMemoryDevice(virQEMUDriverPtr driver,
99cbc7
                              virDomainMemoryDefPtr memdef,
99cbc7
                              bool async)
99cbc7
 {
99cbc7
-    qemuDomainObjPrivatePtr priv = vm->privateData;
99cbc7
     virDomainMemoryDefPtr mem;
99cbc7
     int idx;
99cbc7
-    int rc;
99cbc7
     int ret = -1;
99cbc7
 
99cbc7
     qemuDomainMemoryDeviceAlignSize(vm->def, memdef);
99cbc7
@@ -5724,9 +5671,7 @@ qemuDomainDetachMemoryDevice(virQEMUDriverPtr driver,
99cbc7
     if (!async)
99cbc7
         qemuDomainMarkDeviceForRemoval(vm, &mem->info);
99cbc7
 
99cbc7
-    qemuDomainObjEnterMonitor(driver, vm);
99cbc7
-    rc = qemuMonitorDelDevice(priv->mon, mem->info.alias);
99cbc7
-    if (qemuDomainObjExitMonitor(driver, vm) < 0 || rc < 0)
99cbc7
+    if (qemuDomainDeleteDevice(vm, mem->info.alias) < 0)
99cbc7
         goto cleanup;
99cbc7
 
99cbc7
     if (async) {
99cbc7
@@ -5831,15 +5776,9 @@ qemuDomainHotplugDelVcpu(virQEMUDriverPtr driver,
99cbc7
 
99cbc7
     qemuDomainMarkDeviceAliasForRemoval(vm, vcpupriv->alias);
99cbc7
 
99cbc7
-    qemuDomainObjEnterMonitor(driver, vm);
99cbc7
-
99cbc7
-    rc = qemuMonitorDelDevice(qemuDomainGetMonitor(vm), vcpupriv->alias);
99cbc7
-
99cbc7
-    if (qemuDomainObjExitMonitor(driver, vm) < 0)
99cbc7
-        goto cleanup;
99cbc7
-
99cbc7
-    if (rc < 0) {
99cbc7
-        virDomainAuditVcpu(vm, oldvcpus, oldvcpus - nvcpus, "update", false);
99cbc7
+    if (qemuDomainDeleteDevice(vm, vcpupriv->alias) < 0) {
99cbc7
+        if (virDomainObjIsActive(vm))
99cbc7
+            virDomainAuditVcpu(vm, oldvcpus, oldvcpus - nvcpus, "update", false);
99cbc7
         goto cleanup;
99cbc7
     }
99cbc7
 
99cbc7
@@ -6409,8 +6348,6 @@ qemuDomainDetachInputDevice(virDomainObjPtr vm,
99cbc7
                             virDomainInputDefPtr def,
99cbc7
                             bool async)
99cbc7
 {
99cbc7
-    qemuDomainObjPrivatePtr priv = vm->privateData;
99cbc7
-    virQEMUDriverPtr driver = priv->driver;
99cbc7
     virDomainInputDefPtr input;
99cbc7
     int ret = -1;
99cbc7
     int idx;
99cbc7
@@ -6440,12 +6377,7 @@ qemuDomainDetachInputDevice(virDomainObjPtr vm,
99cbc7
     if (!async)
99cbc7
         qemuDomainMarkDeviceForRemoval(vm, &input->info);
99cbc7
 
99cbc7
-    qemuDomainObjEnterMonitor(driver, vm);
99cbc7
-    if (qemuMonitorDelDevice(priv->mon, input->info.alias) < 0) {
99cbc7
-        ignore_value(qemuDomainObjExitMonitor(driver, vm));
99cbc7
-        goto cleanup;
99cbc7
-    }
99cbc7
-    if (qemuDomainObjExitMonitor(driver, vm) < 0)
99cbc7
+    if (qemuDomainDeleteDevice(vm, input->info.alias) < 0)
99cbc7
         goto cleanup;
99cbc7
 
99cbc7
     if (async) {
99cbc7
@@ -6467,8 +6399,6 @@ qemuDomainDetachVsockDevice(virDomainObjPtr vm,
99cbc7
                             virDomainVsockDefPtr dev,
99cbc7
                             bool async)
99cbc7
 {
99cbc7
-    qemuDomainObjPrivatePtr priv = vm->privateData;
99cbc7
-    virQEMUDriverPtr driver = priv->driver;
99cbc7
     virDomainVsockDefPtr vsock = vm->def->vsock;
99cbc7
     int ret = -1;
99cbc7
 
99cbc7
@@ -6483,12 +6413,7 @@ qemuDomainDetachVsockDevice(virDomainObjPtr vm,
99cbc7
     if (!async)
99cbc7
         qemuDomainMarkDeviceForRemoval(vm, &vsock->info);
99cbc7
 
99cbc7
-    qemuDomainObjEnterMonitor(driver, vm);
99cbc7
-    if (qemuMonitorDelDevice(priv->mon, vsock->info.alias) < 0) {
99cbc7
-        ignore_value(qemuDomainObjExitMonitor(driver, vm));
99cbc7
-        goto cleanup;
99cbc7
-    }
99cbc7
-    if (qemuDomainObjExitMonitor(driver, vm) < 0)
99cbc7
+    if (qemuDomainDeleteDevice(vm, vsock->info.alias) < 0)
99cbc7
         goto cleanup;
99cbc7
 
99cbc7
     if (async) {
99cbc7
-- 
99cbc7
2.21.0
99cbc7