]> git.proxmox.com Git - mirror_edk2.git/commitdiff
OvmfPkg: PciHostBridgeDxe: release resources on driver entry failure
authorLaszlo Ersek <lersek@redhat.com>
Tue, 14 Jul 2015 12:02:15 +0000 (12:02 +0000)
committerlersek <lersek@Edk2>
Tue, 14 Jul 2015 12:02:15 +0000 (12:02 +0000)
The entry point of the driver, InitializePciHostBridge(), leaks resources
(and installed protocols) in the following cases:

- The first root bridge protocol installation fails. In this case, the
  host bridge protocol is left installed, but the driver exits with an
  error.

- The second or a later root bridge protocol installation fails. In this
  case, the host bridge protocol, and all prior root bridge protocols, are
  left installed, even though the driver exits with an error.

Handle errors correctly: roll back / release / uninstall resources when
aborting the driver.

Cc: Jordan Justen <jordan.l.justen@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Regression-tested-by: Gabriel Somlo <somlo@cmu.edu>
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@17959 6f19259b-4bc3-4df7-8a09-765794883524

OvmfPkg/PciHostBridgeDxe/PciHostBridge.c

index 7ca0b6e19b1a6aaecffacd025922aa1c5023d753..a5dbe57eb28548340877d350e7ec37bbbbf8aa2e 100644 (file)
@@ -153,6 +153,32 @@ FreePrivateData:
 }\r
 \r
 \r
+/**\r
+  Uninitialize and free a root bridge set up with InitRootBridge().\r
+\r
+  On return, the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL instance and the device path\r
+  will have been released, freeing RootBus->Handle as well.\r
+\r
+  param[in] RootBus  The private PCI_ROOT_BRIDGE_INSTANCE that has been created\r
+                     with InitRootBridge(), and should be released.\r
+**/\r
+STATIC\r
+VOID\r
+UninitRootBridge (\r
+  IN PCI_ROOT_BRIDGE_INSTANCE *RootBus\r
+  )\r
+{\r
+  EFI_STATUS Status;\r
+\r
+  Status = gBS->UninstallMultipleProtocolInterfaces (RootBus->Handle,\r
+                  &gEfiDevicePathProtocolGuid,      &RootBus->DevicePath,\r
+                  &gEfiPciRootBridgeIoProtocolGuid, &RootBus->Io,\r
+                  NULL);\r
+  ASSERT_EFI_ERROR (Status);\r
+  FreePool (RootBus);\r
+}\r
+\r
+\r
 /**\r
   Entry point of this driver\r
 \r
@@ -174,6 +200,7 @@ InitializePciHostBridge (
   UINTN                       RootBridgeNumber;\r
   PCI_HOST_BRIDGE_INSTANCE    *HostBridge;\r
   PCI_ROOT_BRIDGE_INSTANCE    *RootBus;\r
+  EFI_STATUS                  UninstallStatus;\r
 \r
   mDriverImageHandle = ImageHandle;\r
 \r
@@ -196,8 +223,7 @@ InitializePciHostBridge (
                   NULL\r
                   );\r
   if (EFI_ERROR (Status)) {\r
-    FreePool (HostBridge);\r
-    return EFI_DEVICE_ERROR;\r
+    goto FreeHostBridge;\r
   }\r
 \r
   for (RootBridgeNumber = 0;\r
@@ -209,12 +235,34 @@ InitializePciHostBridge (
                &RootBus\r
                );\r
     if (EFI_ERROR (Status)) {\r
-      return Status;\r
+      goto RollbackProtocols;\r
     }\r
     InsertTailList (&HostBridge->Head, &RootBus->Link);\r
   }\r
 \r
   return EFI_SUCCESS;\r
+\r
+RollbackProtocols:\r
+  while (!IsListEmpty (&HostBridge->Head)) {\r
+    LIST_ENTRY *Entry;\r
+\r
+    Entry = GetFirstNode (&HostBridge->Head);\r
+    RemoveEntryList (Entry);\r
+    RootBus = DRIVER_INSTANCE_FROM_LIST_ENTRY (Entry);\r
+    UninitRootBridge (RootBus);\r
+  }\r
+  UninstallStatus = gBS->UninstallMultipleProtocolInterfaces (\r
+                           HostBridge->HostBridgeHandle,\r
+                           &gEfiPciHostBridgeResourceAllocationProtocolGuid,\r
+                           &HostBridge->ResAlloc,\r
+                           NULL\r
+                           );\r
+  ASSERT_EFI_ERROR (UninstallStatus);\r
+\r
+FreeHostBridge:\r
+  FreePool (HostBridge);\r
+\r
+  return Status;\r
 }\r
 \r
 \r