Experimentation with additional debug logs proved that libvirt would
always end up dispatching the DEVICE_REMOVED event before it had
removed the device from activeDevs (with a *much* greater difference
with managed='yes', since in that case the re-binding of the device
occurred after queuing the device).
Although the case of hostdev devices is the most extreme (since there
is so much involved in tearing down the device), *all* device types
suffer from the same problem - the DEVICE_REMOVED event is queued very
early in the qemuDomainRemove*Device() function for all of them,
resulting in a possibility of any application receiving the event
before libvirt has really finished with the device.
The solution is to save the device's alias (which is the only piece of
info from the device object that is needed for the event) at the
beginning of processing the device removal, and then queue the event
as a final act before returning. Since all of the
qemuDomainRemove*Device() functions (except
qemuDomainRemoveChrDevice()) are now called exclusively from
qemuDomainRemoveDevice() (which selects which of the subordinates to
call in a switch statement based on the type of device), the shortest
route to a solution is to doing the saving of alias, and later
queueing of the event, in the higher level qemuDomainRemoveDevice(),
and just completely remove the event-related code from all the
subordinate functions.
The single exception to this, as mentioned before, is
qemuDomainRemoveChrDevice(), which is still called from somewhere
other than qemuDomainRemoveDevice() (and has a separate arg used to
trigger different behavior when the chr device has targetType ==
GUESTFWD), so it must keep its original behavior intact, and must be
treated differently by qemuDomainRemoveDevice() (similar to the way
that qemuDomainDetachDeviceLive() treats chr and lease devices
differently from all the others).
Resolves: https://bugzilla.redhat.com/1658198
Signed-off-by: Laine Stump <laine@xxxxxxxxx>
---
src/qemu/qemu_hotplug.c | 154 ++++++++++++++++++++--------------------
1 file changed, 78 insertions(+), 76 deletions(-)
diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
index de7a7a2c95..43cc3a314d 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -4501,7 +4501,6 @@ qemuDomainRemoveDiskDevice(virQEMUDriverPtr driver,
{
qemuHotplugDiskSourceDataPtr diskbackend = NULL;
virDomainDeviceDef dev;
- virObjectEventPtr event;
size_t i;
qemuDomainObjPrivatePtr priv = vm->privateData;
int ret = -1;
@@ -4529,9 +4528,6 @@ qemuDomainRemoveDiskDevice(virQEMUDriverPtr driver,
virDomainAuditDisk(vm, disk->src, NULL, "detach", true);
- event = virDomainEventDeviceRemovedNewFromObj(vm, disk->info.alias);
- virObjectEventStateQueue(driver->domainEventState, event);
-
qemuDomainReleaseDeviceAddress(vm, &disk->info, virDomainDiskGetSource(disk));
/* tear down disk security access */
@@ -4555,19 +4551,14 @@ qemuDomainRemoveDiskDevice(virQEMUDriverPtr driver,
static int
-qemuDomainRemoveControllerDevice(virQEMUDriverPtr driver,
- virDomainObjPtr vm,
+qemuDomainRemoveControllerDevice(virDomainObjPtr vm,
virDomainControllerDefPtr controller)
{
- virObjectEventPtr event;
size_t i;
VIR_DEBUG("Removing controller %s from domain %p %s",
controller->info.alias, vm, vm->def->name);
- event = virDomainEventDeviceRemovedNewFromObj(vm, controller->info.alias);
- virObjectEventStateQueue(driver->domainEventState, event);
-
for (i = 0; i < vm->def->ncontrollers; i++) {
if (vm->def->controllers[i] == controller) {
virDomainControllerRemove(vm->def, i);
@@ -4589,7 +4580,6 @@ qemuDomainRemoveMemoryDevice(virQEMUDriverPtr driver,
qemuDomainObjPrivatePtr priv = vm->privateData;
unsigned long long oldmem = virDomainDefGetMemoryTotal(vm->def);
unsigned long long newmem = oldmem - mem->size;
- virObjectEventPtr event;
char *backendAlias = NULL;
int rc;
int idx;
@@ -4611,9 +4601,6 @@ qemuDomainRemoveMemoryDevice(virQEMUDriverPtr driver,
if (rc < 0)
return -1;
- event = virDomainEventDeviceRemovedNewFromObj(vm, mem->info.alias);
- virObjectEventStateQueue(driver->domainEventState, event);
-
if ((idx = virDomainMemoryFindByDef(vm->def, mem)) >= 0)
virDomainMemoryRemove(vm->def, idx);
@@ -4693,7 +4680,6 @@ qemuDomainRemoveHostDevice(virQEMUDriverPtr driver,
{
virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
virDomainNetDefPtr net = NULL;
- virObjectEventPtr event;
size_t i;
int ret = -1;
qemuDomainObjPrivatePtr priv = vm->privateData;
@@ -4737,9 +4723,6 @@ qemuDomainRemoveHostDevice(virQEMUDriverPtr driver,
goto cleanup;
}
- event = virDomainEventDeviceRemovedNewFromObj(vm, hostdev->info->alias);
- virObjectEventStateQueue(driver->domainEventState, event);
-
if (hostdev->parent.type == VIR_DOMAIN_DEVICE_NET) {
net = hostdev->parent.data.net;
@@ -4818,7 +4801,6 @@ qemuDomainRemoveNetDevice(virQEMUDriverPtr driver,
{
virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
qemuDomainObjPrivatePtr priv = vm->privateData;
- virObjectEventPtr event;
char *hostnet_name = NULL;
char *charDevAlias = NULL;
size_t i;
@@ -4863,9 +4845,6 @@ qemuDomainRemoveNetDevice(virQEMUDriverPtr driver,
virDomainAuditNet(vm, net, NULL, "detach", true);
- event = virDomainEventDeviceRemovedNewFromObj(vm, net->info.alias);
- virObjectEventStateQueue(driver->domainEventState, event);
-
for (i = 0; i < vm->def->nnets; i++) {
if (vm->def->nets[i] == net) {
virDomainNetRemove(vm->def, i);
@@ -4948,11 +4927,20 @@ qemuDomainRemoveChrDevice(virQEMUDriverPtr driver,
if (qemuDomainNamespaceTeardownChardev(vm, chr) < 0)
VIR_WARN("Unable to remove chr device from /dev");
+ qemuDomainReleaseDeviceAddress(vm, &chr->info, NULL);
+ qemuDomainChrRemove(vm->def, chr);
+
+ /* NB: all other qemuDomainRemove*Device() functions omit the
+ * sending of the DEVICE_REMOVED event, because they are *only*
+ * called from qemuDomainRemoveDevice(), and that function sends
+ * the DEVICE_REMOVED event for them, this function is different -
+ * it is called from other places than just
+ * qemuDomainRemoveDevice(), so it must send the DEVICE_REMOVED
+ * event itself.
+ */
event = virDomainEventDeviceRemovedNewFromObj(vm, chr->info.alias);
virObjectEventStateQueue(driver->domainEventState, event);
- qemuDomainReleaseDeviceAddress(vm, &chr->info, NULL);
- qemuDomainChrRemove(vm->def, chr);
virDomainChrDefFree(chr);
ret = 0;
@@ -4967,7 +4955,6 @@ qemuDomainRemoveRNGDevice(virQEMUDriverPtr driver,
virDomainObjPtr vm,
virDomainRNGDefPtr rng)
{
- virObjectEventPtr event;
char *charAlias = NULL;
char *objAlias = NULL;
qemuDomainObjPrivatePtr priv = vm->privateData;
@@ -5016,9 +5003,6 @@ qemuDomainRemoveRNGDevice(virQEMUDriverPtr driver,
if (qemuDomainNamespaceTeardownRNG(vm, rng) < 0)
VIR_WARN("Unable to remove RNG device from /dev");
- event = virDomainEventDeviceRemovedNewFromObj(vm, rng->info.alias);
- virObjectEventStateQueue(driver->domainEventState, event);
-
if ((idx = virDomainRNGFind(vm->def, rng)) >= 0)
virDomainRNGRemove(vm->def, idx);
qemuDomainReleaseDeviceAddress(vm, &rng->info, NULL);
@@ -5043,7 +5027,6 @@ qemuDomainRemoveShmemDevice(virQEMUDriverPtr driver,
char *charAlias = NULL;
char *memAlias = NULL;
qemuDomainObjPrivatePtr priv = vm->privateData;
- virObjectEventPtr event = NULL;
VIR_DEBUG("Removing shmem device %s from domain %p %s",
shmem->info.alias, vm, vm->def->name);
@@ -5071,9 +5054,6 @@ qemuDomainRemoveShmemDevice(virQEMUDriverPtr driver,
if (rc < 0)
goto cleanup;
- event = virDomainEventDeviceRemovedNewFromObj(vm, shmem->info.alias);
- virObjectEventStateQueue(driver->domainEventState, event);
-
if ((idx = virDomainShmemDefFind(vm->def, shmem)) >= 0)
virDomainShmemDefRemove(vm->def, idx);
qemuDomainReleaseDeviceAddress(vm, &shmem->info, NULL);
@@ -5089,17 +5069,12 @@ qemuDomainRemoveShmemDevice(virQEMUDriverPtr driver,
static int
-qemuDomainRemoveWatchdog(virQEMUDriverPtr driver,
- virDomainObjPtr vm,
+qemuDomainRemoveWatchdog(virDomainObjPtr vm,
virDomainWatchdogDefPtr watchdog)
{
- virObjectEventPtr event = NULL;
-
VIR_DEBUG("Removing watchdog %s from domain %p %s",
watchdog->info.alias, vm, vm->def->name);
- event = virDomainEventDeviceRemovedNewFromObj(vm, watchdog->info.alias);
- virObjectEventStateQueue(driver->domainEventState, event);
qemuDomainReleaseDeviceAddress(vm, &watchdog->info, NULL);
virDomainWatchdogDefFree(vm->def->watchdog);
vm->def->watchdog = NULL;
@@ -5111,16 +5086,11 @@ static int
qemuDomainRemoveInputDevice(virDomainObjPtr vm,
virDomainInputDefPtr dev)
{
- qemuDomainObjPrivatePtr priv = vm->privateData;
- virQEMUDriverPtr driver = priv->driver;
- virObjectEventPtr event = NULL;
size_t i;
VIR_DEBUG("Removing input device %s from domain %p %s",
dev->info.alias, vm, vm->def->name);
- event = virDomainEventDeviceRemovedNewFromObj(vm, dev->info.alias);
- virObjectEventStateQueue(driver->domainEventState, event);
for (i = 0; i < vm->def->ninputs; i++) {
if (vm->def->inputs[i] == dev)
break;
@@ -5145,15 +5115,9 @@ static int
qemuDomainRemoveVsockDevice(virDomainObjPtr vm,
virDomainVsockDefPtr dev)
{
- qemuDomainObjPrivatePtr priv = vm->privateData;
- virQEMUDriverPtr driver = priv->driver;
- virObjectEventPtr event = NULL;
-
VIR_DEBUG("Removing vsock device %s from domain %p %s",
dev->info.alias, vm, vm->def->name);
- event = virDomainEventDeviceRemovedNewFromObj(vm, dev->info.alias);
- virObjectEventStateQueue(driver->domainEventState, event);
qemuDomainReleaseDeviceAddress(vm, &dev->info, NULL);
virDomainVsockDefFree(vm->def->vsock);
vm->def->vsock = NULL;
@@ -5167,7 +5131,6 @@ qemuDomainRemoveRedirdevDevice(virQEMUDriverPtr driver,
virDomainRedirdevDefPtr dev)
{
qemuDomainObjPrivatePtr priv = vm->privateData;
- virObjectEventPtr event;
char *charAlias = NULL;
ssize_t idx;
int ret = -1;
@@ -5192,9 +5155,6 @@ qemuDomainRemoveRedirdevDevice(virQEMUDriverPtr driver,
virDomainAuditRedirdev(vm, dev, "detach", true);
- event = virDomainEventDeviceRemovedNewFromObj(vm, dev->info.alias);
- virObjectEventStateQueue(driver->domainEventState, event);
-
if ((idx = virDomainRedirdevDefFind(vm->def, dev)) >= 0)
virDomainRedirdevDefRemove(vm->def, idx);
qemuDomainReleaseDeviceAddress(vm, &dev->info, NULL);
@@ -5285,50 +5245,88 @@ qemuDomainRemoveDevice(virQEMUDriverPtr driver,
virDomainObjPtr vm,
virDomainDeviceDefPtr dev)
{
- int ret = -1;
+ virDomainDeviceInfoPtr info;
+ virObjectEventPtr event;
+ VIR_AUTOFREE(char *)alias = NULL;
+
+ if (!(info = virDomainDeviceGetInfo(dev))) {
+ /*
+ * This should never happen, since all of the device types in
+ * the switch cases that end with a "break" instead of a
+ * return have a virDeviceInfo in them.