Mark McLoughlin 7c2073
From d04ac8624f5fabe7587982796f2e2161220b0fcc Mon Sep 17 00:00:00 2001
Mark McLoughlin f1ac00
From: Mark McLoughlin <markmc@redhat.com>
Mark McLoughlin f1ac00
Date: Thu, 17 Sep 2009 15:32:45 +0100
Mark McLoughlin f1ac00
Subject: [PATCH] Fix leak in PCI hostdev hot-unplug
Mark McLoughlin f1ac00
Mark McLoughlin f1ac00
* src/qemu_driver.c: sync the hostdev hot-unplug code with the disk/net
Mark McLoughlin f1ac00
  code.
Mark McLoughlin f1ac00
Mark McLoughlin 7c2073
(cherry-picked from commit a70da51ff76ed860bfc0cdee2e1d556da997c557)
Mark McLoughlin 7c2073
Mark McLoughlin f1ac00
Fedora-patch: libvirt-fix-pci-hostdev-hotunplug-leak.patch
Mark McLoughlin f1ac00
---
Mark McLoughlin f1ac00
 src/qemu_driver.c |   20 +++++++++++++-------
Mark McLoughlin f1ac00
 1 files changed, 13 insertions(+), 7 deletions(-)
Mark McLoughlin f1ac00
Mark McLoughlin f1ac00
diff --git a/src/qemu_driver.c b/src/qemu_driver.c
Mark McLoughlin f1ac00
index de31581..2ddcdc0 100644
Mark McLoughlin f1ac00
--- a/src/qemu_driver.c
Mark McLoughlin f1ac00
+++ b/src/qemu_driver.c
Mark McLoughlin f1ac00
@@ -6206,14 +6206,20 @@ static int qemudDomainDetachHostPciDevice(virConnectPtr conn,
Mark McLoughlin f1ac00
         pciFreeDevice(conn, pci);
Mark McLoughlin f1ac00
     }
Mark McLoughlin f1ac00
 
Mark McLoughlin f1ac00
-    if (i != --vm->def->nhostdevs)
Mark McLoughlin f1ac00
-        memmove(&vm->def->hostdevs[i],
Mark McLoughlin f1ac00
-                &vm->def->hostdevs[i+1],
Mark McLoughlin f1ac00
-                sizeof(*vm->def->hostdevs) * (vm->def->nhostdevs-i));
Mark McLoughlin f1ac00
-    if (VIR_REALLOC_N(vm->def->hostdevs, vm->def->nhostdevs) < 0) {
Mark McLoughlin f1ac00
-        virReportOOMError(conn);
Mark McLoughlin f1ac00
-        ret = -1;
Mark McLoughlin f1ac00
+    if (vm->def->nhostdevs > 1) {
Mark McLoughlin f1ac00
+        memmove(vm->def->hostdevs + i,
Mark McLoughlin f1ac00
+                vm->def->hostdevs + i + 1,
Mark McLoughlin f1ac00
+                sizeof(*vm->def->hostdevs) *
Mark McLoughlin f1ac00
+                (vm->def->nhostdevs - (i + 1)));
Mark McLoughlin f1ac00
+        vm->def->nhostdevs--;
Mark McLoughlin f1ac00
+        if (VIR_REALLOC_N(vm->def->hostdevs, vm->def->nhostdevs) < 0) {
Mark McLoughlin f1ac00
+            /* ignore, harmless */
Mark McLoughlin f1ac00
+        }
Mark McLoughlin f1ac00
+    } else {
Mark McLoughlin f1ac00
+        VIR_FREE(vm->def->hostdevs);
Mark McLoughlin f1ac00
+        vm->def->nhostdevs = 0;
Mark McLoughlin f1ac00
     }
Mark McLoughlin f1ac00
+    virDomainHostdevDefFree(detach);
Mark McLoughlin f1ac00
 
Mark McLoughlin f1ac00
     return ret;
Mark McLoughlin f1ac00
 }
Mark McLoughlin f1ac00
-- 
Mark McLoughlin f1ac00
1.6.2.5
Mark McLoughlin f1ac00