public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Zeng, Star" <star.zeng@intel.com>
To: "Bi, Dandan" <dandan.bi@intel.com>,
	"edk2-devel@lists.01.org" <edk2-devel@lists.01.org>
Cc: Max Knutsen <maknutse@microsoft.com>,
	"Wu, Hao A" <hao.a.wu@intel.com>,
	Michael Turner <Michael.Turner@microsoft.com>,
	"Gao, Liming" <liming.gao@intel.com>,
	"Zeng, Star" <star.zeng@intel.com>
Subject: Re: [patch V3] MdeModulePkg/ReportStatusCodeLib: Avoid using AllocatePool if possible
Date: Mon, 18 Feb 2019 09:51:38 +0000	[thread overview]
Message-ID: <0C09AFA07DD0434D9E2A0C6AEB0483104026EBA9@shsmsx102.ccr.corp.intel.com> (raw)
In-Reply-To: <20190218024325.62204-1-dandan.bi@intel.com>

Hi Dandan,

/DxeReportStatusCodeLib/ReportStatusCodeLib.c:

The " Retrieve the current TPL " code block is not needed also when using stack buffer, so it can be also moved to else condition.

And the code block below is redundant, so it can be removed.
  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"));
      return EFI_OUT_OF_RESOURCES;
    }
    StatusCodeData = (EFI_STATUS_CODE_DATA  *)Buffer;
  }

The code for /RuntimeDxeReportStatusCodeLib/ReportStatusCodeLib.c can be also refined.

To be easily understand, check https://github.com/lzeng14/edk2/tree/ReportStatusCode for reference.


Thanks,
Star
-----Original Message-----
From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of Dandan Bi
Sent: Monday, February 18, 2019 10:43 AM
To: edk2-devel@lists.01.org
Cc: Max Knutsen <maknutse@microsoft.com>; Wu, Hao A <hao.a.wu@intel.com>; Michael Turner <Michael.Turner@microsoft.com>; Gao, Liming <liming.gao@intel.com>; Zeng, Star <star.zeng@intel.com>
Subject: [edk2] [patch V3] MdeModulePkg/ReportStatusCodeLib: Avoid using AllocatePool if possible

From: Max Knutsen <maknutse@microsoft.com>

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;
  }

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 <star.zeng@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Hao Wu <hao.a.wu@intel.com>
Cc: Michael Turner <Michael.Turner@microsoft.com>
Cc: Liming Gao <liming.gao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Dandan Bi <dandan.bi@intel.com>
---
 .../DxeReportStatusCodeLib/ReportStatusCodeLib.c  | 15 +++++++++------
 .../ReportStatusCodeLib.c                         |  9 +++++++--
 2 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/MdeModulePkg/Library/DxeReportStatusCodeLib/ReportStatusCodeLib.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];
 
   ASSERT (!((ExtendedData == NULL) && (ExtendedDataSize != 0)));
   ASSERT (!((ExtendedData != NULL) && (ExtendedDataSize == 0)));
 
-  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);
 
   StatusCodeData = NULL;
-  if (Tpl <= TPL_NOTIFY) {
+  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;
+  } else if (Tpl <= TPL_NOTIFY){
     //
-    // Allocate space for the Status Code Header and its buffer
+    // If Buffer is not big enough, allocate space for the Status Code 
+ Header and its buffer
     //
     gBS->AllocatePool (EfiBootServicesData, sizeof (EFI_STATUS_CODE_DATA) + ExtendedDataSize, (VOID **)&StatusCodeData);
   }
 
   if (StatusCodeData == NULL) {
diff --git a/MdeModulePkg/Library/RuntimeDxeReportStatusCodeLib/ReportStatusCodeLib.c b/MdeModulePkg/Library/RuntimeDxeReportStatusCodeLib/ReportStatusCodeLib.c
index b73103517a..75e2f88ad2 100644
--- a/MdeModulePkg/Library/RuntimeDxeReportStatusCodeLib/ReportStatusCodeLib.c
+++ b/MdeModulePkg/Library/RuntimeDxeReportStatusCodeLib/ReportStatusCod
+++ eLib.c
@@ -635,11 +635,14 @@ ReportStatusCodeEx (
   if (mHaveExitedBootServices) {
     if (sizeof (EFI_STATUS_CODE_DATA) + ExtendedDataSize > MAX_EXTENDED_DATA_SIZE) {
       return EFI_OUT_OF_RESOURCES;
     }
     StatusCodeData = (EFI_STATUS_CODE_DATA *) StatusCodeBuffer;
-  } else  {
+  } else if (sizeof (EFI_STATUS_CODE_DATA) + ExtendedDataSize > MAX_EXTENDED_DATA_SIZE) {
+    //
+    // Only use AllocatePool for larger ExtendedData.
+    //
     if (gBS == NULL || gBS->AllocatePool == NULL || gBS->FreePool == NULL) {
       return EFI_UNSUPPORTED;
     }
 
     //
@@ -648,10 +651,12 @@ ReportStatusCodeEx (
     StatusCodeData = NULL;
     gBS->AllocatePool (EfiBootServicesData, sizeof (EFI_STATUS_CODE_DATA) + ExtendedDataSize, (VOID **)&StatusCodeData);
     if (StatusCodeData == NULL) {
       return EFI_OUT_OF_RESOURCES;
     }
+  } else {
+    StatusCodeData = (EFI_STATUS_CODE_DATA *) StatusCodeBuffer;
   }
 
   //
   // Fill in the extended data header
   //
@@ -678,11 +683,11 @@ ReportStatusCodeEx (
   Status = InternalReportStatusCode (Type, Value, Instance, CallerId, StatusCodeData);
 
   //
   // Free the allocated buffer
   //
-  if (!mHaveExitedBootServices) {
+  if (StatusCodeData != (EFI_STATUS_CODE_DATA *) StatusCodeBuffer) {
     gBS->FreePool (StatusCodeData);
   }
 
   return Status;
 }
--
2.18.0.windows.1

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel


      reply	other threads:[~2019-02-18  9:51 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-18  2:43 [patch V3] MdeModulePkg/ReportStatusCodeLib: Avoid using AllocatePool if possible Dandan Bi
2019-02-18  9:51 ` Zeng, Star [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0C09AFA07DD0434D9E2A0C6AEB0483104026EBA9@shsmsx102.ccr.corp.intel.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox