audk/MdeModulePkg
Aaron Young a6648418c1 MdeModulePkg: Optimize BmExpandPartitionDevicePath
Reference: https://github.com/tianocore/edk2/pull/4892

BmExpandPartitionDevicePath is called to expand "short-form" device paths
which are commonly used with OS boot options. To expand a device path, it
calls EfiBootManagerConnectAll to connect all the possible BlockIo
devices in the system to search for a matching partition. However, this
is sometimes unnecessary on certain platforms (such as OVMF/QEMU) because
the boot devices are previously explicity connected
(See: ConnectDevicesFromQemu).  EfiBootManagerConnectAll calls are
extremely costly in terms of boot time and resources and should be avoided
whenever feasible.

(

OVMF call tree:

  PlatformBootManagerAfterConsole()         [OvmfPkg/Library/PlatformBootManagerLib/BdsPlatform.c]
    PlatformBdsConnectSequence()            [OvmfPkg/Library/PlatformBootManagerLib/BdsPlatform.c]
      ConnectDevicesFromQemu()              [OvmfPkg/Library/QemuBootOrderLib/QemuBootOrderLib.c]
        ...
    EfiBootManagerRefreshAllBootOption()    [MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c]
      ...
    SetBootOrderFromQemu()                  [OvmfPkg/Library/QemuBootOrderLib/QemuBootOrderLib.c]
      Match()                               [OvmfPkg/Library/QemuBootOrderLib/QemuBootOrderLib.c]
        EfiBootManagerGetLoadOptionBuffer() [MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c]
          BmGetNextLoadOptionBuffer()       [MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c]
            BmGetNextLoadOptionDevicePath() [MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c]
              BmExpandPartitionDevicePath() [MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c]

)

Therefore optimize BmExpandPartitionDevicePath to first search the
existing BlockIo handles for a match. If a match is not found, then
fallback to the original code to call EfiBootManagerConnectAll and search
again. Thus, this optimization should be extremely low-risk given the
fallback to previous behavior.

NOTE: The existing optimization in the code to use a "HDDP" variable to
save the last matched device paths does not cover the first time a boot
option is expanded (i.e. before the "HDDP" is created) nor when the device
configuration has changed (resulting in the boot device moving to a
different location in the PCI Bus/Dev hierarchy). This new optimization
covers both of these cases on requisite platforms which explicity connect
boot devices.

In our testing on OVMF/QEMU VMs with dozens of configured vnic devices,
these extraneous calls to EfiBootManagerConnectAll from
BmExpandPartitionDevicePath were found to cause many seconds (or even
minutes) of additional VM boot time in some cases - due to the vnics
being unnecessarily connected.

Cc: Zhichao Gao zhichao.gao@intel.com
Cc: Ray Ni ray.ni@intel.com
Signed-off-by: Aaron Young <aaron.young@oracle.com>
Message-Id: <20231010150644.37857-1-Aaron.Young@oracle.com>
Reviewed-by: Zhichao Gao <zhichao.gao@intel.com>
[lersek@redhat.com: add OVMF call tree to commit message]
2023-10-24 13:00:18 +00:00
..
Application MdeModulePkg/CapsuleApp: Add EFIAPI to CompareFileNameInAlphabet() 2023-05-06 17:48:49 +00:00
Bus MdeModulePkg: UsbRndis: get rid of magic values 2023-10-16 22:49:33 +00:00
Core MdeModulePkg: Fix misspelling 2023-09-26 20:03:23 +00:00
Include MdeModulePkg: UsbRndis: get rid of magic values 2023-10-16 22:49:33 +00:00
Library MdeModulePkg: Optimize BmExpandPartitionDevicePath 2023-10-24 13:00:18 +00:00
Logo MdeModulePkg/Logo: Add LoongArch64 architecture. 2022-10-14 02:16:33 +00:00
Test MdeModulePkg: Add more PciHostBridgeLib gmock support 2023-05-10 00:39:13 +00:00
Universal MdeModulePkg/SmbiosDxe: Fix BcdRevision is not match with SMBIOS version 2023-09-26 23:56:43 +00:00
MdeModulePkg.ci.yaml MdeModulePkg: CI: Add PrEval entry 2023-10-23 20:17:52 +00:00
MdeModulePkg.dec MdeModulePkg/Rng: Add GUID to describe unsafe Rng algorithms 2023-09-08 09:48:55 +00:00
MdeModulePkg.dsc MdeModulePkg: Duplicate BaseRngLibTimerLib to MdeModulePkg 2023-09-08 09:48:55 +00:00
MdeModulePkg.uni MdeModulePkg: Add TraceHubDebugSysTLib library 2023-05-11 07:26:38 +00:00
MdeModulePkgExtra.uni