From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.93; helo=mga11.intel.com; envelope-from=star.zeng@intel.com; receiver=edk2-devel@lists.01.org Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 61B96201B045D for ; Mon, 18 Feb 2019 01:51:43 -0800 (PST) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 18 Feb 2019 01:51:42 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.58,384,1544515200"; d="scan'208";a="300567853" Received: from fmsmsx105.amr.corp.intel.com ([10.18.124.203]) by orsmga005.jf.intel.com with ESMTP; 18 Feb 2019 01:51:41 -0800 Received: from fmsmsx112.amr.corp.intel.com (10.18.116.6) by FMSMSX105.amr.corp.intel.com (10.18.124.203) with Microsoft SMTP Server (TLS) id 14.3.408.0; Mon, 18 Feb 2019 01:51:41 -0800 Received: from shsmsx101.ccr.corp.intel.com (10.239.4.153) by FMSMSX112.amr.corp.intel.com (10.18.116.6) with Microsoft SMTP Server (TLS) id 14.3.408.0; Mon, 18 Feb 2019 01:51:40 -0800 Received: from shsmsx102.ccr.corp.intel.com ([169.254.2.207]) by SHSMSX101.ccr.corp.intel.com ([169.254.1.110]) with mapi id 14.03.0415.000; Mon, 18 Feb 2019 17:51:39 +0800 From: "Zeng, Star" To: "Bi, Dandan" , "edk2-devel@lists.01.org" CC: Max Knutsen , "Wu, Hao A" , Michael Turner , "Gao, Liming" , "Zeng, Star" Thread-Topic: [edk2] [patch V3] MdeModulePkg/ReportStatusCodeLib: Avoid using AllocatePool if possible Thread-Index: AQHUxzPC/LYI9ZEmjUWuG4Q1tbx0sKXlR/lQ Date: Mon, 18 Feb 2019 09:51:38 +0000 Message-ID: <0C09AFA07DD0434D9E2A0C6AEB0483104026EBA9@shsmsx102.ccr.corp.intel.com> References: <20190218024325.62204-1-dandan.bi@intel.com> In-Reply-To: <20190218024325.62204-1-dandan.bi@intel.com> Accept-Language: zh-CN, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.239.127.40] MIME-Version: 1.0 Subject: Re: [patch V3] MdeModulePkg/ReportStatusCodeLib: Avoid using AllocatePool if possible X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 18 Feb 2019 09:51:43 -0000 Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Hi Dandan, /DxeReportStatusCodeLib/ReportStatusCodeLib.c: The " Retrieve the current TPL " code block is not needed also when using s= tack buffer, so it can be also moved to else condition. And the code block below is redundant, so it can be removed. if (StatusCodeData =3D=3D NULL) { // // If a buffer could not be allocated, then see if the local variable B= uffer can be used // if (ExtendedDataSize > (MAX_EXTENDED_DATA_SIZE - sizeof (EFI_STATUS_COD= E_DATA))) { // // The local variable Buffer not large enough to hold the extended da= ta associated // with the status code being reported. // DEBUG ((EFI_D_ERROR, "Status code extended data is too large to be re= ported!\n")); return EFI_OUT_OF_RESOURCES; } StatusCodeData =3D (EFI_STATUS_CODE_DATA *)Buffer; } The code for /RuntimeDxeReportStatusCodeLib/ReportStatusCodeLib.c can be al= so refined. To be easily understand, check https://github.com/lzeng14/edk2/tree/ReportS= tatusCode for reference. Thanks, Star -----Original Message----- From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of Dand= an Bi Sent: Monday, February 18, 2019 10:43 AM To: edk2-devel@lists.01.org Cc: Max Knutsen ; Wu, Hao A ; M= ichael Turner ; Gao, Liming ; Zeng, Star Subject: [edk2] [patch V3] MdeModulePkg/ReportStatusCodeLib: Avoid using Al= locatePool if possible From: Max Knutsen REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D1114 V2: simplify the code logic. update if (!mHaveExitedBootServices && (StatusCodeData !=3D (EFI_STATUS_CODE_DATA *) StatusCodeBuffer)) { gBS->FreePool (StatusCodeData); } to if (StatusCodeData !=3D (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 =3D=3D NULL || gBS->AllocatePool =3D=3D NULL || gBS->FreePool =3D= =3D NULL) { return EFI_UNSUPPORTED; } When report status code with ExtendedData data, and the extended data can f= it 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 --- .../DxeReportStatusCodeLib/ReportStatusCodeLib.c | 15 +++++++++------ .../ReportStatusCodeLib.c | 9 +++++++-- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/MdeModulePkg/Library/DxeReportStatusCodeLib/ReportStatusCodeLi= b.c b/MdeModulePkg/Library/DxeReportStatusCodeLib/ReportStatusCodeLib.c index b28dc5c3bb..3bca0a94ee 100644 --- a/MdeModulePkg/Library/DxeReportStatusCodeLib/ReportStatusCodeLib.c +++ b/MdeModulePkg/Library/DxeReportStatusCodeLib/ReportStatusCodeLib.c @@ -494,24 +494,27 @@ ReportStatusCodeEx ( UINT64 Buffer[(MAX_EXTENDED_DATA_SIZE / sizeof (UINT64)) = + 1]; =20 ASSERT (!((ExtendedData =3D=3D NULL) && (ExtendedDataSize !=3D 0))); ASSERT (!((ExtendedData !=3D NULL) && (ExtendedDataSize =3D=3D 0))); =20 - if (gBS =3D=3D NULL || gBS->AllocatePool =3D=3D NULL || gBS->FreePool = =3D=3D NULL) { - return EFI_UNSUPPORTED; - } - // // Retrieve the current TPL // Tpl =3D gBS->RaiseTPL (TPL_HIGH_LEVEL); gBS->RestoreTPL (Tpl); =20 StatusCodeData =3D NULL; - if (Tpl <=3D TPL_NOTIFY) { + if (ExtendedDataSize <=3D (MAX_EXTENDED_DATA_SIZE - sizeof (EFI_STATUS_C= ODE_DATA))) { + // + // Use Buffer instead of allocating if possible. + // + StatusCodeData =3D (EFI_STATUS_CODE_DATA *)Buffer; } else if (gBS =3D= =3D=20 + NULL || gBS->AllocatePool =3D=3D NULL || gBS->FreePool =3D=3D NULL) { + return EFI_UNSUPPORTED; + } else if (Tpl <=3D TPL_NOTIFY){ // - // Allocate space for the Status Code Header and its buffer + // If Buffer is not big enough, allocate space for the Status Code=20 + Header and its buffer // gBS->AllocatePool (EfiBootServicesData, sizeof (EFI_STATUS_CODE_DATA) = + ExtendedDataSize, (VOID **)&StatusCodeData); } =20 if (StatusCodeData =3D=3D NULL) { diff --git a/MdeModulePkg/Library/RuntimeDxeReportStatusCodeLib/ReportStatu= sCodeLib.c b/MdeModulePkg/Library/RuntimeDxeReportStatusCodeLib/ReportStatu= sCodeLib.c index b73103517a..75e2f88ad2 100644 --- a/MdeModulePkg/Library/RuntimeDxeReportStatusCodeLib/ReportStatusCodeLi= b.c +++ b/MdeModulePkg/Library/RuntimeDxeReportStatusCodeLib/ReportStatusCod +++ eLib.c @@ -635,11 +635,14 @@ ReportStatusCodeEx ( if (mHaveExitedBootServices) { if (sizeof (EFI_STATUS_CODE_DATA) + ExtendedDataSize > MAX_EXTENDED_DA= TA_SIZE) { return EFI_OUT_OF_RESOURCES; } StatusCodeData =3D (EFI_STATUS_CODE_DATA *) StatusCodeBuffer; - } else { + } else if (sizeof (EFI_STATUS_CODE_DATA) + ExtendedDataSize > MAX_EXTEND= ED_DATA_SIZE) { + // + // Only use AllocatePool for larger ExtendedData. + // if (gBS =3D=3D NULL || gBS->AllocatePool =3D=3D NULL || gBS->FreePool = =3D=3D NULL) { return EFI_UNSUPPORTED; } =20 // @@ -648,10 +651,12 @@ ReportStatusCodeEx ( StatusCodeData =3D NULL; gBS->AllocatePool (EfiBootServicesData, sizeof (EFI_STATUS_CODE_DATA) = + ExtendedDataSize, (VOID **)&StatusCodeData); if (StatusCodeData =3D=3D NULL) { return EFI_OUT_OF_RESOURCES; } + } else { + StatusCodeData =3D (EFI_STATUS_CODE_DATA *) StatusCodeBuffer; } =20 // // Fill in the extended data header // @@ -678,11 +683,11 @@ ReportStatusCodeEx ( Status =3D InternalReportStatusCode (Type, Value, Instance, CallerId, St= atusCodeData); =20 // // Free the allocated buffer // - if (!mHaveExitedBootServices) { + if (StatusCodeData !=3D (EFI_STATUS_CODE_DATA *) StatusCodeBuffer) { gBS->FreePool (StatusCodeData); } =20 return Status; } -- 2.18.0.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel