From c22f52c5e79b9782648576efb8382bb04da60b5b Mon Sep 17 00:00:00 2001 From: Max Knutsen Date: Tue, 4 Sep 2018 17:04:35 +0000 Subject: [PATCH] MdeModulePkg/ReportStatusCodeLib: Avoid using AllocatePool if possible REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1114 V2: simplify the code logic. update if (!mHaveExitedBootServices && (StatusCodeData != (EFI_STATUS_CODE_DATA *) StatusCodeBuffer)) { gBS->FreePool (StatusCodeData); } to if (StatusCodeData != (EFI_STATUS_CODE_DATA *) StatusCodeBuffer) { gBS->FreePool (StatusCodeData); } V3: And the code below into the else condition (stack buffer is not enough) in /DxeReportStatusCodeLib/ReportStatusCodeLib.c if (gBS == NULL || gBS->AllocatePool == NULL || gBS->FreePool == NULL) { return EFI_UNSUPPORTED; } V4: Refine code logic. When report status code with ExtendedData data, and the extended data can fit in the local static buffer, there is no need to use AllocatePool to hold the ExtendedData data. This patch is just to do the enhancement to avoid using AllocatePool. Cc: Star Zeng Cc: Jian J Wang Cc: Hao Wu Cc: Michael Turner Cc: Liming Gao Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Dandan Bi Reviewed-by: Liming Gao Reviewed-by: Star Zeng --- .../ReportStatusCodeLib.c | 43 +++++++++---------- .../ReportStatusCodeLib.c | 14 +++--- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/MdeModulePkg/Library/DxeReportStatusCodeLib/ReportStatusCodeLib.c b/MdeModulePkg/Library/DxeReportStatusCodeLib/ReportStatusCodeLib.c index b28dc5c3bb..307b95de7c 100644 --- a/MdeModulePkg/Library/DxeReportStatusCodeLib/ReportStatusCodeLib.c +++ b/MdeModulePkg/Library/DxeReportStatusCodeLib/ReportStatusCodeLib.c @@ -496,37 +496,34 @@ ReportStatusCodeEx ( ASSERT (!((ExtendedData == NULL) && (ExtendedDataSize != 0))); ASSERT (!((ExtendedData != NULL) && (ExtendedDataSize == 0))); - if (gBS == NULL || gBS->AllocatePool == NULL || gBS->FreePool == NULL) { - return EFI_UNSUPPORTED; - } + if (ExtendedDataSize <= (MAX_EXTENDED_DATA_SIZE - sizeof (EFI_STATUS_CODE_DATA))) { + // + // Use Buffer instead of allocating if possible. + // + StatusCodeData = (EFI_STATUS_CODE_DATA *)Buffer; + } else { + if (gBS == NULL || gBS->AllocatePool == NULL || gBS->FreePool == NULL) { + return EFI_UNSUPPORTED; + } - // - // Retrieve the current TPL - // - Tpl = gBS->RaiseTPL (TPL_HIGH_LEVEL); - gBS->RestoreTPL (Tpl); + // + // Retrieve the current TPL + // + Tpl = gBS->RaiseTPL (TPL_HIGH_LEVEL); + gBS->RestoreTPL (Tpl); + + if (Tpl > TPL_NOTIFY) { + return EFI_OUT_OF_RESOURCES; + } - StatusCodeData = NULL; - if (Tpl <= TPL_NOTIFY) { // // Allocate space for the Status Code Header and its buffer // + StatusCodeData = NULL; gBS->AllocatePool (EfiBootServicesData, sizeof (EFI_STATUS_CODE_DATA) + ExtendedDataSize, (VOID **)&StatusCodeData); - } - - if (StatusCodeData == NULL) { - // - // If a buffer could not be allocated, then see if the local variable Buffer can be used - // - if (ExtendedDataSize > (MAX_EXTENDED_DATA_SIZE - sizeof (EFI_STATUS_CODE_DATA))) { - // - // The local variable Buffer not large enough to hold the extended data associated - // with the status code being reported. - // - DEBUG ((EFI_D_ERROR, "Status code extended data is too large to be reported!\n")); + if (StatusCodeData == NULL) { return EFI_OUT_OF_RESOURCES; } - StatusCodeData = (EFI_STATUS_CODE_DATA *)Buffer; } // diff --git a/MdeModulePkg/Library/RuntimeDxeReportStatusCodeLib/ReportStatusCodeLib.c b/MdeModulePkg/Library/RuntimeDxeReportStatusCodeLib/ReportStatusCodeLib.c index b73103517a..9b79854538 100644 --- a/MdeModulePkg/Library/RuntimeDxeReportStatusCodeLib/ReportStatusCodeLib.c +++ b/MdeModulePkg/Library/RuntimeDxeReportStatusCodeLib/ReportStatusCodeLib.c @@ -632,12 +632,16 @@ ReportStatusCodeEx ( ASSERT (!((ExtendedData == NULL) && (ExtendedDataSize != 0))); ASSERT (!((ExtendedData != NULL) && (ExtendedDataSize == 0))); - if (mHaveExitedBootServices) { - if (sizeof (EFI_STATUS_CODE_DATA) + ExtendedDataSize > MAX_EXTENDED_DATA_SIZE) { + if (ExtendedDataSize <= (MAX_EXTENDED_DATA_SIZE - sizeof (EFI_STATUS_CODE_DATA))) { + // + // Use Buffer instead of allocating if possible. + // + StatusCodeData = (EFI_STATUS_CODE_DATA *) StatusCodeBuffer; + } else { + if (mHaveExitedBootServices) { return EFI_OUT_OF_RESOURCES; } - StatusCodeData = (EFI_STATUS_CODE_DATA *) StatusCodeBuffer; - } else { + if (gBS == NULL || gBS->AllocatePool == NULL || gBS->FreePool == NULL) { return EFI_UNSUPPORTED; } @@ -680,7 +684,7 @@ ReportStatusCodeEx ( // // Free the allocated buffer // - if (!mHaveExitedBootServices) { + if (StatusCodeData != (EFI_STATUS_CODE_DATA *) StatusCodeBuffer) { gBS->FreePool (StatusCodeData); }