* This adds a single pci device to the global
  * device list and adds sysfs and procfs entries
  */
-void __devinit pci_bus_add_device(struct pci_dev *dev)
+int __devinit pci_bus_add_device(struct pci_dev *dev)
 {
-       device_add(&dev->dev);
+       int retval;
+       retval = device_add(&dev->dev);
+       if (retval)
+               return retval;
 
        down_write(&pci_bus_sem);
        list_add_tail(&dev->global_list, &pci_devices);
 
        pci_proc_attach_device(dev);
        pci_create_sysfs_dev_files(dev);
+       return 0;
 }
 
 /**
 void __devinit pci_bus_add_devices(struct pci_bus *bus)
 {
        struct pci_dev *dev;
+       int retval;
 
        list_for_each_entry(dev, &bus->devices, bus_list) {
                /*
                 */
                if (!list_empty(&dev->global_list))
                        continue;
-               pci_bus_add_device(dev);
+               retval = pci_bus_add_device(dev);
+               if (retval)
+                       dev_err(&dev->dev, "Error adding device, continuing\n");
        }
 
        list_for_each_entry(dev, &bus->devices, bus_list) {
                               list_add_tail(&dev->subordinate->node,
                                               &dev->bus->children);
                               up_write(&pci_bus_sem);
-                      }
+                       }
                        pci_bus_add_devices(dev->subordinate);
-
-                       sysfs_create_link(&dev->subordinate->class_dev.kobj, &dev->dev.kobj, "bridge");
+                       retval = sysfs_create_link(&dev->subordinate->class_dev.kobj,
+                                                  &dev->dev.kobj, "bridge");
+                       if (retval)
+                               dev_err(&dev->dev, "Error creating sysfs "
+                                       "bridge symlink, continuing...\n");
                }
        }
 }
 
        struct pci_bus *bus = temp->bus;
        struct pci_dev *dev;
        int func;
+       int retval;
        u8 hdr_type;
+
        if (!pci_read_config_byte(temp, PCI_HEADER_TYPE, &hdr_type)) {
                temp->hdr_type = hdr_type & 0x7f;
                if (!pci_find_slot(bus->number, temp->devfn)) {
                                dbg("New device on %s function %x:%x\n",
                                        bus->name, temp->devfn >> 3,
                                        temp->devfn & 7);
-                               pci_bus_add_device(dev);
-                               add_slot(dev);
+                               retval = pci_bus_add_device(dev);
+                               if (retval)
+                                       dev_err(&dev->dev, "error adding "
+                                               "device, continuing.\n");
+                               else
+                                       add_slot(dev);
                        }
                }
                /* multifunction device? */
                                        dbg("New device on %s function %x:%x\n",
                                                bus->name, temp->devfn >> 3,
                                                temp->devfn & 7);
-                                       pci_bus_add_device(dev);
-                                       add_slot(dev);
+                                       retval = pci_bus_add_device(dev);
+                                       if (retval)
+                                               dev_err(&dev->dev, "error adding "
+                                                       "device, continuing.\n");
+                                       else
+                                               add_slot(dev);
                                }
                        }
                }
 
                subdevice=PCI_ANY_ID, class=0, class_mask=0;
        unsigned long driver_data=0;
        int fields=0;
+       int retval = 0;
 
        fields = sscanf(buf, "%x %x %x %x %x %x %lux",
                        &vendor, &device, &subvendor, &subdevice,
        spin_unlock(&pdrv->dynids.lock);
 
        if (get_driver(&pdrv->driver)) {
-               driver_attach(&pdrv->driver);
+               retval = driver_attach(&pdrv->driver);
                put_driver(&pdrv->driver);
        }
 
+       if (retval)
+               return retval;
        return count;
 }
 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
 
                const char *buf, size_t count)
 {
        struct pci_dev *pdev = to_pci_dev(dev);
+       int retval = 0;
 
        /* this can crash the machine when done on the "wrong" device */
        if (!capable(CAP_SYS_ADMIN))
                pci_disable_device(pdev);
 
        if (*buf == '1')
-               pci_enable_device(pdev);
+               retval = pci_enable_device(pdev);
 
+       if (retval)
+               return retval;
        return count;
 }
 
        return pci_mmap_page_range(pdev, vma, mmap_type, 0);
 }
 
+/**
+ * pci_remove_resource_files - cleanup resource files
+ * @dev: dev to cleanup
+ *
+ * If we created resource files for @dev, remove them from sysfs and
+ * free their resources.
+ */
+static void
+pci_remove_resource_files(struct pci_dev *pdev)
+{
+       int i;
+
+       for (i = 0; i < PCI_ROM_RESOURCE; i++) {
+               struct bin_attribute *res_attr;
+
+               res_attr = pdev->res_attr[i];
+               if (res_attr) {
+                       sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
+                       kfree(res_attr);
+               }
+       }
+}
+
 /**
  * pci_create_resource_files - create resource files in sysfs for @dev
  * @dev: dev in question
  *
  * Walk the resources in @dev creating files for each resource available.
  */
-static void
-pci_create_resource_files(struct pci_dev *pdev)
+static int pci_create_resource_files(struct pci_dev *pdev)
 {
        int i;
+       int retval;
 
        /* Expose the PCI resources from this device as files */
        for (i = 0; i < PCI_ROM_RESOURCE; i++) {
                        res_attr->size = pci_resource_len(pdev, i);
                        res_attr->mmap = pci_mmap_resource;
                        res_attr->private = &pdev->resource[i];
-                       sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
-               }
-       }
-}
-
-/**
- * pci_remove_resource_files - cleanup resource files
- * @dev: dev to cleanup
- *
- * If we created resource files for @dev, remove them from sysfs and
- * free their resources.
- */
-static void
-pci_remove_resource_files(struct pci_dev *pdev)
-{
-       int i;
-
-       for (i = 0; i < PCI_ROM_RESOURCE; i++) {
-               struct bin_attribute *res_attr;
-
-               res_attr = pdev->res_attr[i];
-               if (res_attr) {
-                       sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
-                       kfree(res_attr);
+                       retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
+                       if (retval) {
+                               pci_remove_resource_files(pdev);
+                               return retval;
+                       }
+               } else {
+                       return -ENOMEM;
                }
        }
+       return 0;
 }
 #else /* !HAVE_PCI_MMAP */
-static inline void pci_create_resource_files(struct pci_dev *dev) { return; }
+static inline int pci_create_resource_files(struct pci_dev *dev) { return 0; }
 static inline void pci_remove_resource_files(struct pci_dev *dev) { return; }
 #endif /* HAVE_PCI_MMAP */
 
        .write = pci_write_config,
 };
 
-int pci_create_sysfs_dev_files (struct pci_dev *pdev)
+int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
 {
+       struct bin_attribute *rom_attr = NULL;
+       int retval;
+
        if (!sysfs_initialized)
                return -EACCES;
 
        if (pdev->cfg_size < 4096)
-               sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
+               retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
        else
-               sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
+               retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
+       if (retval)
+               goto err;
 
-       pci_create_resource_files(pdev);
+       retval = pci_create_resource_files(pdev);
+       if (retval)
+               goto err_bin_file;
 
        /* If the device has a ROM, try to expose it in sysfs. */
        if (pci_resource_len(pdev, PCI_ROM_RESOURCE)) {
-               struct bin_attribute *rom_attr;
-               
                rom_attr = kzalloc(sizeof(*rom_attr), GFP_ATOMIC);
                if (rom_attr) {
                        pdev->rom_attr = rom_attr;
                        rom_attr->attr.owner = THIS_MODULE;
                        rom_attr->read = pci_read_rom;
                        rom_attr->write = pci_write_rom;
-                       sysfs_create_bin_file(&pdev->dev.kobj, rom_attr);
+                       retval = sysfs_create_bin_file(&pdev->dev.kobj, rom_attr);
+                       if (retval)
+                               goto err_rom;
+               } else {
+                       retval = -ENOMEM;
+                       goto err_bin_file;
                }
        }
        /* add platform-specific attributes */
        pcibios_add_platform_entries(pdev);
-       
+
        return 0;
+
+err_rom:
+       kfree(rom_attr);
+err_bin_file:
+       if (pdev->cfg_size < 4096)
+               sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
+       else
+               sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
+err:
+       return retval;
 }
 
 /**
 static int __init pci_sysfs_init(void)
 {
        struct pci_dev *pdev = NULL;
-       
+       int retval;
+
        sysfs_initialized = 1;
-       for_each_pci_dev(pdev)
-               pci_create_sysfs_dev_files(pdev);
+       for_each_pci_dev(pdev) {
+               retval = pci_create_sysfs_dev_files(pdev);
+               if (retval)
+                       return retval;
+       }
 
        return 0;
 }
 
 static void find_aer_service(struct pci_dev *dev,
                struct find_aer_service_data *data)
 {
-       device_for_each_child(&dev->dev, data, find_aer_service_iter);
+       int retval;
+       retval = device_for_each_child(&dev->dev, data, find_aer_service_iter);
 }
 
 static pci_ers_result_t reset_link(struct pcie_device *aerdev,
 
 
 int pcie_port_device_suspend(struct pci_dev *dev, pm_message_t state)
 {
-       device_for_each_child(&dev->dev, &state, suspend_iter);
-       return 0;
+       return device_for_each_child(&dev->dev, &state, suspend_iter);
 }
 
 static int resume_iter(struct device *dev, void *data)
 
 int pcie_port_device_resume(struct pci_dev *dev)
 {
-       device_for_each_child(&dev->dev, NULL, resume_iter);
-       return 0;
+       return device_for_each_child(&dev->dev, NULL, resume_iter);
 }
 #endif
 
 
 {
        struct aer_broadcast_data result_data =
                        {error, PCI_ERS_RESULT_CAN_RECOVER};
+       int retval;
 
-       device_for_each_child(&dev->dev, &result_data, error_detected_iter);
+       /* can not fail */
+       retval = device_for_each_child(&dev->dev, &result_data, error_detected_iter);
 
        return result_data.result;
 }
 static pci_ers_result_t pcie_portdrv_mmio_enabled(struct pci_dev *dev)
 {
        pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
+       int retval;
 
-       device_for_each_child(&dev->dev, &status, mmio_enabled_iter);
+       /* get true return value from &status */
+       retval = device_for_each_child(&dev->dev, &status, mmio_enabled_iter);
        return status;
 }
 
 static pci_ers_result_t pcie_portdrv_slot_reset(struct pci_dev *dev)
 {
        pci_ers_result_t status;
+       int retval;
 
        /* If fatal, restore cfg space for possible link reset at upstream */
        if (dev->error_state == pci_channel_io_frozen) {
                pci_enable_pcie_error_reporting(dev);
        }
 
-       device_for_each_child(&dev->dev, &status, slot_reset_iter);
+       /* get true return value from &status */
+       retval = device_for_each_child(&dev->dev, &status, slot_reset_iter);
 
        return status;
 }
 
 static void pcie_portdrv_err_resume(struct pci_dev *dev)
 {
-       device_for_each_child(&dev->dev, NULL, resume_iter);
+       int retval;
+       /* nothing to do with error value, if it ever happens */
+       retval = device_for_each_child(&dev->dev, NULL, resume_iter);
 }
 
 /*
 
 {
        struct pci_bus *child;
        int i;
+       int retval;
 
        /*
         * Allocate a new bus, and inherit stuff from the parent..
 
        child->class_dev.class = &pcibus_class;
        sprintf(child->class_dev.class_id, "%04x:%02x", pci_domain_nr(child), busnr);
-       class_device_register(&child->class_dev);
-       class_device_create_file(&child->class_dev, &class_device_attr_cpuaffinity);
+       retval = class_device_register(&child->class_dev);
+       if (retval)
+               goto error_register;
+       retval = class_device_create_file(&child->class_dev,
+                                         &class_device_attr_cpuaffinity);
+       if (retval)
+               goto error_file_create;
 
        /*
         * Set up the primary, secondary and subordinate
        bridge->subordinate = child;
 
        return child;
+
+error_file_create:
+       class_device_unregister(&child->class_dev);
+error_register:
+       kfree(child);
+       return NULL;
 }
 
 struct pci_bus * __devinit pci_add_new_bus(struct pci_bus *parent, struct pci_dev *dev, int busnr)
 
 struct pci_dev * pci_scan_single_device(struct pci_bus *bus, int devfn);
 void pci_device_add(struct pci_dev *dev, struct pci_bus *bus);
 unsigned int pci_scan_child_bus(struct pci_bus *bus);
-void pci_bus_add_device(struct pci_dev *dev);
+int __must_check pci_bus_add_device(struct pci_dev *dev);
 void pci_read_bridge_bases(struct pci_bus *child);
 struct resource *pci_find_parent_resource(const struct pci_dev *dev, struct resource *res);
 int pci_get_interrupt_pin(struct pci_dev *dev, struct pci_dev **bridge);