9ae3a8
From e346f15b5c72e3cf58fbf1af4a89c929dea783ad Mon Sep 17 00:00:00 2001
9ae3a8
From: Marcel Apfelbaum <marcel.a@redhat.com>
9ae3a8
Date: Tue, 28 Oct 2014 14:13:15 +0100
9ae3a8
Subject: [PATCH 2/9] hw/pci: fix error flow in pci multifunction init
9ae3a8
9ae3a8
Message-id: <1414505595-17009-1-git-send-email-marcel.a@redhat.com>
9ae3a8
Patchwork-id: 61953
9ae3a8
O-Subject: [RHEL-7.1 qemu-kvm PATCH] hw/pci: fix error flow in pci multifunction init
9ae3a8
Bugzilla: 1049734
9ae3a8
RH-Acked-by: Amos Kong <akong@redhat.com>
9ae3a8
RH-Acked-by: Markus Armbruster <armbru@redhat.com>
9ae3a8
RH-Acked-by: Michael S. Tsirkin <mst@redhat.com>
9ae3a8
9ae3a8
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1049734
9ae3a8
Brew: http://brewweb.devel.redhat.com/brew/taskinfo?taskID=8164686
9ae3a8
Upstream-status: 306077640a652e090779498aadbeb0c605feaacd
9ae3a8
9ae3a8
Scenario:
9ae3a8
  - There is a non multifunction pci device A on 00:0X.0.
9ae3a8
  - Hot-plug another multifunction pci device B at 00:0X.1.
9ae3a8
  - The operation will fail of course.
9ae3a8
  - Try to hot-plug the B device 2-3 more times, qemu will crash.
9ae3a8
9ae3a8
Reason: The error flow leaves the B's address space into global address spaces
9ae3a8
list, but the device object is freed. Fixed that.
9ae3a8
9ae3a8
Signed-off-by: Marcel Apfelbaum <marcel.a@redhat.com>
9ae3a8
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
9ae3a8
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
9ae3a8
(cherry picked from commit 306077640a652e090779498aadbeb0c605feaacd)
9ae3a8
Signed-off-by: Marcel Apfelbaum <marcel.a@redhat.com>
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
9ae3a8
Conflicts:
9ae3a8
	hw/pci/pci.c
9ae3a8
        - The patch moves a function within a file and the function's
9ae3a8
          content is different from upstream.
9ae3a8
        - The upstream does not call anymore to qemu_free_irqs, but
9ae3a8
          the downstream version still does. We need to check that
9ae3a8
          the irq is allocated before calling it.
9ae3a8
9ae3a8
scripts/git-backport-diff:
9ae3a8
        001/1:[0021] [FC] 'hw/pci: fix error flow in pci multifunction init'
9ae3a8
9ae3a8
Signed-off-by: Marcel Apfelbaum <marcel.a@redhat.com>
9ae3a8
---
9ae3a8
 hw/pci/pci.c | 33 ++++++++++++++++++---------------
9ae3a8
 1 file changed, 18 insertions(+), 15 deletions(-)
9ae3a8
---
9ae3a8
 hw/pci/pci.c | 33 ++++++++++++++++++---------------
9ae3a8
 1 file changed, 18 insertions(+), 15 deletions(-)
9ae3a8
9ae3a8
diff --git a/hw/pci/pci.c b/hw/pci/pci.c
9ae3a8
index d166ab0..703b111 100644
9ae3a8
--- a/hw/pci/pci.c
9ae3a8
+++ b/hw/pci/pci.c
9ae3a8
@@ -784,6 +784,23 @@ static void pci_config_free(PCIDevice *pci_dev)
9ae3a8
     g_free(pci_dev->used);
9ae3a8
 }
9ae3a8
 
9ae3a8
+static void do_pci_unregister_device(PCIDevice *pci_dev)
9ae3a8
+{
9ae3a8
+    if (pci_dev->irq) {
9ae3a8
+        qemu_free_irqs(pci_dev->irq);
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    pci_dev->bus->devices[pci_dev->devfn] = NULL;
9ae3a8
+    pci_config_free(pci_dev);
9ae3a8
+
9ae3a8
+    if (!pci_dev->bus->dma_context_fn) {
9ae3a8
+        address_space_destroy(&pci_dev->bus_master_as);
9ae3a8
+        memory_region_destroy(&pci_dev->bus_master_enable_region);
9ae3a8
+        g_free(pci_dev->dma);
9ae3a8
+        pci_dev->dma = NULL;
9ae3a8
+    }
9ae3a8
+}
9ae3a8
+
9ae3a8
 /* -1 for devfn means auto assign */
9ae3a8
 static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
9ae3a8
                                          const char *name, int devfn)
9ae3a8
@@ -852,7 +869,7 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
9ae3a8
         pci_init_mask_bridge(pci_dev);
9ae3a8
     }
9ae3a8
     if (pci_init_multifunction(bus, pci_dev)) {
9ae3a8
-        pci_config_free(pci_dev);
9ae3a8
+        do_pci_unregister_device(pci_dev);
9ae3a8
         return NULL;
9ae3a8
     }
9ae3a8
 
9ae3a8
@@ -868,20 +885,6 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
9ae3a8
     return pci_dev;
9ae3a8
 }
9ae3a8
 
9ae3a8
-static void do_pci_unregister_device(PCIDevice *pci_dev)
9ae3a8
-{
9ae3a8
-    qemu_free_irqs(pci_dev->irq);
9ae3a8
-    pci_dev->bus->devices[pci_dev->devfn] = NULL;
9ae3a8
-    pci_config_free(pci_dev);
9ae3a8
-
9ae3a8
-    if (!pci_dev->bus->dma_context_fn) {
9ae3a8
-        address_space_destroy(&pci_dev->bus_master_as);
9ae3a8
-        memory_region_destroy(&pci_dev->bus_master_enable_region);
9ae3a8
-        g_free(pci_dev->dma);
9ae3a8
-        pci_dev->dma = NULL;
9ae3a8
-    }
9ae3a8
-}
9ae3a8
-
9ae3a8
 static void pci_unregister_io_regions(PCIDevice *pci_dev)
9ae3a8
 {
9ae3a8
     PCIIORegion *r;
9ae3a8
-- 
9ae3a8
1.8.3.1
9ae3a8