PCI: Remove from bus_list and release resources in pci_release_dev()
authorYinghai Lu <yinghai@kernel.org>
Sat, 30 Nov 2013 22:40:29 +0000 (14:40 -0800)
committerBjorn Helgaas <bhelgaas@google.com>
Wed, 18 Dec 2013 20:53:40 +0000 (13:53 -0700)
Previously we removed the pci_dev from the bus_list and released its
resources in pci_destroy_dev().  But that's too early: it's possible to
call pci_destroy_dev() twice for the same device (e.g., via sysfs), and
that will cause an oops when we try to remove it from bus_list the second
time.

We should remove it from the bus_list only when the last reference to the
pci_dev has been released, i.e., in pci_release_dev().

[bhelgaas: changelog]
Signed-off-by: Yinghai Lu <yinghai@kernel.org>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
drivers/pci/probe.c
drivers/pci/remove.c

index 173a9cf4593e51a01561c596a596afeb631eb4b6..12ec56c9a913fd3b22040b28f1cdcbf91e008c57 100644 (file)
@@ -1154,6 +1154,18 @@ static void pci_release_capabilities(struct pci_dev *dev)
        pci_free_cap_save_buffers(dev);
 }
 
+static void pci_free_resources(struct pci_dev *dev)
+{
+       int i;
+
+       pci_cleanup_rom(dev);
+       for (i = 0; i < PCI_NUM_RESOURCES; i++) {
+               struct resource *res = dev->resource + i;
+               if (res->parent)
+                       release_resource(res);
+       }
+}
+
 /**
  * pci_release_dev - free a pci device structure when all users of it are finished.
  * @dev: device that's been disconnected
@@ -1163,9 +1175,14 @@ static void pci_release_capabilities(struct pci_dev *dev)
  */
 static void pci_release_dev(struct device *dev)
 {
-       struct pci_dev *pci_dev;
+       struct pci_dev *pci_dev = to_pci_dev(dev);
+
+       down_write(&pci_bus_sem);
+       list_del(&pci_dev->bus_list);
+       up_write(&pci_bus_sem);
+
+       pci_free_resources(pci_dev);
 
-       pci_dev = to_pci_dev(dev);
        pci_release_capabilities(pci_dev);
        pci_release_of_node(pci_dev);
        pcibios_release_device(pci_dev);
index 692f4c39ac48d6e086d879da6275fb05da4f4e21..f452148e6d55ac3fef4c634e1255572c952d7886 100644 (file)
@@ -3,20 +3,6 @@
 #include <linux/pci-aspm.h>
 #include "pci.h"
 
-static void pci_free_resources(struct pci_dev *dev)
-{
-       int i;
-
-       msi_remove_pci_irq_vectors(dev);
-
-       pci_cleanup_rom(dev);
-       for (i = 0; i < PCI_NUM_RESOURCES; i++) {
-               struct resource *res = dev->resource + i;
-               if (res->parent)
-                       release_resource(res);
-       }
-}
-
 static void pci_stop_dev(struct pci_dev *dev)
 {
        pci_pme_active(dev, false);
@@ -36,11 +22,6 @@ static void pci_destroy_dev(struct pci_dev *dev)
 {
        device_del(&dev->dev);
 
-       down_write(&pci_bus_sem);
-       list_del(&dev->bus_list);
-       up_write(&pci_bus_sem);
-
-       pci_free_resources(dev);
        put_device(&dev->dev);
 }