Skip to content

Commit

Permalink
MdeModulePkg: Optimize CoreInstallMultipleProtocolInterfaces
Browse files Browse the repository at this point in the history
CoreLocateDevicePath is used in CoreInstallMultipleProtocolInterfaces to
check if a Device Path Protocol instance with the same device path is
alreay installed.
CoreLocateDevicePath is a generic API, and would introduce some
unnecessary overhead for such usage.

The optimization is:
1. Implement IsDevicePathInstalled to loop all the Device Path
   Protocols installed and check if any of them matchs the given device
   path.
2. Replace CoreLocateDevicePath with IsDevicePathInstalled in
   CoreInstallMultipleProtocolInterfaces.

This optimization could save several seconds in PCI enumeration on a
system with many PCI devices.

Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Dandan Bi <dandan.bi@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Signed-off-by: Zhi Jin <zhi.jin@intel.com>
Reviewed-by: Ray Ni <ray.ni@intel.com>
Reviewed-by: Liming Gao <gaoliming@byosoft.com.cn>
  • Loading branch information
zjin7 authored and mergify[bot] committed Dec 1, 2023
1 parent 26d484d commit 5340219
Showing 1 changed file with 72 additions and 17 deletions.
89 changes: 72 additions & 17 deletions MdeModulePkg/Core/Dxe/Hand/Handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,66 @@ CoreFindProtocolInterface (
return Prot;
}

/**
Check if the given device path is already installed.
@param DevicePath The given device path
@retval TRUE The device path is already installed
@retval FALSE The device path is not installed
**/
BOOLEAN
IsDevicePathInstalled (
IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
)
{
UINTN SourceSize;
UINTN Size;
BOOLEAN Found;
LIST_ENTRY *Link;
PROTOCOL_ENTRY *ProtEntry;
PROTOCOL_INTERFACE *Prot;

if (DevicePath == NULL) {
return FALSE;
}

Found = FALSE;
SourceSize = GetDevicePathSize (DevicePath);
ASSERT (SourceSize >= END_DEVICE_PATH_LENGTH);

CoreAcquireProtocolLock ();
//
// Look up the protocol entry
//
ProtEntry = CoreFindProtocolEntry (&gEfiDevicePathProtocolGuid, FALSE);
if (ProtEntry == NULL) {
goto Done;
}

for (Link = ProtEntry->Protocols.ForwardLink; Link != &ProtEntry->Protocols; Link = Link->ForwardLink) {
//
// Loop on the DevicePathProtocol interfaces
//
Prot = CR (Link, PROTOCOL_INTERFACE, ByProtocol, PROTOCOL_INTERFACE_SIGNATURE);

//
// Check if DevicePath is same as this interface
//
Size = GetDevicePathSize (Prot->Interface);
ASSERT (Size >= END_DEVICE_PATH_LENGTH);
if ((Size == SourceSize) && (CompareMem (DevicePath, Prot->Interface, Size - END_DEVICE_PATH_LENGTH) == 0)) {
Found = TRUE;
break;
}
}

Done:
CoreReleaseProtocolLock ();
return Found;
}

/**
Removes an event from a register protocol notify list on a protocol.
Expand Down Expand Up @@ -510,15 +570,13 @@ CoreInstallMultipleProtocolInterfaces (
...
)
{
VA_LIST Args;
EFI_STATUS Status;
EFI_GUID *Protocol;
VOID *Interface;
EFI_TPL OldTpl;
UINTN Index;
EFI_HANDLE OldHandle;
EFI_HANDLE DeviceHandle;
EFI_DEVICE_PATH_PROTOCOL *DevicePath;
VA_LIST Args;
EFI_STATUS Status;
EFI_GUID *Protocol;
VOID *Interface;
EFI_TPL OldTpl;
UINTN Index;
EFI_HANDLE OldHandle;

if (Handle == NULL) {
return EFI_INVALID_PARAMETER;
Expand Down Expand Up @@ -548,14 +606,11 @@ CoreInstallMultipleProtocolInterfaces (
//
// Make sure you are installing on top a device path that has already been added.
//
if (CompareGuid (Protocol, &gEfiDevicePathProtocolGuid)) {
DeviceHandle = NULL;
DevicePath = Interface;
Status = CoreLocateDevicePath (&gEfiDevicePathProtocolGuid, &DevicePath, &DeviceHandle);
if (!EFI_ERROR (Status) && (DeviceHandle != NULL) && IsDevicePathEnd (DevicePath)) {
Status = EFI_ALREADY_STARTED;
continue;
}
if (CompareGuid (Protocol, &gEfiDevicePathProtocolGuid) &&
IsDevicePathInstalled (Interface))
{
Status = EFI_ALREADY_STARTED;
continue;
}

//
Expand Down

0 comments on commit 5340219

Please sign in to comment.