[edk2-devel] [PATCH] MdeModulePkg:fix memory wasting issue

Yanming Zhu posted 1 patch 3 years, 5 months ago
Failed in applying to current master (apply log)
MdeModulePkg/Universal/PCD/Pei/Service.c | 34 ++++++++++++++++--------
MdeModulePkg/Universal/PCD/Pei/Service.h | 14 +++++++++-
2 files changed, 36 insertions(+), 12 deletions(-)
[edk2-devel] [PATCH] MdeModulePkg:fix memory wasting issue
Posted by Yanming Zhu 3 years, 5 months ago
REF:https://bugzilla.tianocore.org/show_bug.cgi?id=2964

Use GuidHob instead of AllocatePool.
In PEI phase, variable is read only.
So, GuidHob can be used to cache the variable data
for the following usage.
This GuidHob is the private GUID hob for PcdPei.
So I add one internal guid for this guidhob.
GuidHob data include VariableGuid, VariableName and VariableBuffer.
One GuidHob is for one EFI variable.

Signed-off-by: Yanming Zhu <zhuyanming@byosoft.com.cn>
---
 MdeModulePkg/Universal/PCD/Pei/Service.c | 34 ++++++++++++++++--------
 MdeModulePkg/Universal/PCD/Pei/Service.h | 14 +++++++++-
 2 files changed, 36 insertions(+), 12 deletions(-)

diff --git a/MdeModulePkg/Universal/PCD/Pei/Service.c b/MdeModulePkg/Universal/PCD/Pei/Service.c
index 5b037353ad..38ed4546d6 100644
--- a/MdeModulePkg/Universal/PCD/Pei/Service.c
+++ b/MdeModulePkg/Universal/PCD/Pei/Service.c
@@ -472,9 +472,12 @@ GetHiiVariable (
   OUT UINTN               *VariableSize
   )
 {
-  UINTN      Size;
-  EFI_STATUS Status;
-  VOID       *Buffer;
+  UINTN                           Size;
+  EFI_STATUS                      Status;
+  TEST_DATA                       TestData;
+  EFI_HOB_GUID_TYPE               *GuidHob;
+  TEST_DATA                       *DataPtr;
+  UINT32                          DataSize;
   EFI_PEI_READ_ONLY_VARIABLE2_PPI *VariablePpi;
 
   Status = PeiServicesLocatePpi (&gEfiPeiReadOnlyVariable2PpiGuid, 0, NULL, (VOID **) &VariablePpi);
@@ -489,23 +492,32 @@ GetHiiVariable (
                           &Size,
                           NULL
                           );
-
   if (Status == EFI_BUFFER_TOO_SMALL) {
-    Status = PeiServicesAllocatePool (Size, &Buffer);
-    ASSERT_EFI_ERROR (Status);
-
+    //Status = PeiServicesAllocatePool (Size, &Buffer);
+    //create guid hob
+    BuildGuidDataHob (
+        (EFI_GUID *)VariableGuid,
+        &TestData,
+        sizeof (TestData)
+        );
+     // find guid hob
+    GuidHob = GetFirstGuidHob ((EFI_GUID *)VariableGuid);
+    if (GuidHob != NULL) {
+      DataPtr  = GET_GUID_HOB_DATA (GuidHob);
+      DataSize = GET_GUID_HOB_DATA_SIZE (GuidHob);
+    }
     Status = VariablePpi->GetVariable (
                               VariablePpi,
                               (UINT16 *) VariableName,
                               (EFI_GUID *) VariableGuid,
                               NULL,
-                              &Size,
-                              Buffer
+                              &DataSize,
+                              DataPtr
                               );
     ASSERT_EFI_ERROR (Status);
 
-    *VariableSize = Size;
-    *VariableData = Buffer;
+    *VariableSize = DataSize;
+    *VariableData = DataPtr;
 
     return EFI_SUCCESS;
   }
diff --git a/MdeModulePkg/Universal/PCD/Pei/Service.h b/MdeModulePkg/Universal/PCD/Pei/Service.h
index 547094fe8a..2fa57a768e 100644
--- a/MdeModulePkg/Universal/PCD/Pei/Service.h
+++ b/MdeModulePkg/Universal/PCD/Pei/Service.h
@@ -26,7 +26,6 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #include <Library/PcdLib.h>
 #include <Library/BaseMemoryLib.h>
 #include <Library/MemoryAllocationLib.h>
-
 //
 // Please make sure the PCD Serivce PEIM Version is consistent with
 // the version of the generated PEIM PCD Database by build tool.
@@ -40,6 +39,19 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
   #error "Please make sure the version of PCD PEIM Service and the generated PCD PEI Database match."
 #endif
 
+/**
+  GuidHob can be used to cache the variable data for the following usage.
+  This GuidHob is the private GUID hob for PcdPei
+  One GuidHob will be for one EFI variable.
+
+  This structure contains three members:VariableGuid,VariableName,VariableBuffer.
+**/
+typedef struct  {
+  IN  CONST EFI_GUID      *VariableGuid;
+  IN  UINT16              *VariableName;
+  VOID                    *VariableBuffer;
+} TEST_DATA;
+
 /**
   Retrieve additional information associated with a PCD token in the default token space.
 
-- 
2.28.0.windows.1




-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#66651): https://edk2.groups.io/g/devel/message/66651
Mute This Topic: https://groups.io/mt/77836833/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-


回复: [edk2-devel] [PATCH] MdeModulePkg:fix memory wasting issue
Posted by gaoliming 3 years, 5 months ago
Yanming:
  This patch doesn't catch variable data in guided hob. Please create GUID hob to store the variable guid/name/data. 

Thanks
Liming
> -----邮件原件-----
> 发件人: bounce+27952+66651+4905953+8761045@groups.io
> <bounce+27952+66651+4905953+8761045@groups.io> 代表 Yanming Zhu
> 发送时间: 2020年10月26日 17:59
> 收件人: devel@edk2.groups.io
> 主题: [edk2-devel] [PATCH] MdeModulePkg:fix memory wasting issue
> 
> REF:https://bugzilla.tianocore.org/show_bug.cgi?id=2964
> 
> Use GuidHob instead of AllocatePool.
> In PEI phase, variable is read only.
> So, GuidHob can be used to cache the variable data
> for the following usage.
> This GuidHob is the private GUID hob for PcdPei.
> So I add one internal guid for this guidhob.
> GuidHob data include VariableGuid, VariableName and VariableBuffer.
> One GuidHob is for one EFI variable.
> 
> Signed-off-by: Yanming Zhu <zhuyanming@byosoft.com.cn>
> ---
>  MdeModulePkg/Universal/PCD/Pei/Service.c | 34
> ++++++++++++++++--------
>  MdeModulePkg/Universal/PCD/Pei/Service.h | 14 +++++++++-
>  2 files changed, 36 insertions(+), 12 deletions(-)
> 
> diff --git a/MdeModulePkg/Universal/PCD/Pei/Service.c
> b/MdeModulePkg/Universal/PCD/Pei/Service.c
> index 5b037353ad..38ed4546d6 100644
> --- a/MdeModulePkg/Universal/PCD/Pei/Service.c
> +++ b/MdeModulePkg/Universal/PCD/Pei/Service.c
> @@ -472,9 +472,12 @@ GetHiiVariable (
>    OUT UINTN               *VariableSize
>    )
>  {
> -  UINTN      Size;
> -  EFI_STATUS Status;
> -  VOID       *Buffer;
> +  UINTN                           Size;
> +  EFI_STATUS                      Status;
> +  TEST_DATA                       TestData;
> +  EFI_HOB_GUID_TYPE               *GuidHob;
> +  TEST_DATA                       *DataPtr;
> +  UINT32                          DataSize;
>    EFI_PEI_READ_ONLY_VARIABLE2_PPI *VariablePpi;
> 
>    Status = PeiServicesLocatePpi (&gEfiPeiReadOnlyVariable2PpiGuid, 0,
> NULL, (VOID **) &VariablePpi);
> @@ -489,23 +492,32 @@ GetHiiVariable (
>                            &Size,
>                            NULL
>                            );
> -
>    if (Status == EFI_BUFFER_TOO_SMALL) {
> -    Status = PeiServicesAllocatePool (Size, &Buffer);
> -    ASSERT_EFI_ERROR (Status);
> -
> +    //Status = PeiServicesAllocatePool (Size, &Buffer);
> +    //create guid hob
> +    BuildGuidDataHob (
> +        (EFI_GUID *)VariableGuid,
> +        &TestData,
> +        sizeof (TestData)
> +        );
> +     // find guid hob
> +    GuidHob = GetFirstGuidHob ((EFI_GUID *)VariableGuid);
> +    if (GuidHob != NULL) {
> +      DataPtr  = GET_GUID_HOB_DATA (GuidHob);
> +      DataSize = GET_GUID_HOB_DATA_SIZE (GuidHob);
> +    }
>      Status = VariablePpi->GetVariable (
>                                VariablePpi,
>                                (UINT16 *) VariableName,
>                                (EFI_GUID *) VariableGuid,
>                                NULL,
> -                              &Size,
> -                              Buffer
> +                              &DataSize,
> +                              DataPtr
>                                );
>      ASSERT_EFI_ERROR (Status);
> 
> -    *VariableSize = Size;
> -    *VariableData = Buffer;
> +    *VariableSize = DataSize;
> +    *VariableData = DataPtr;
> 
>      return EFI_SUCCESS;
>    }
> diff --git a/MdeModulePkg/Universal/PCD/Pei/Service.h
> b/MdeModulePkg/Universal/PCD/Pei/Service.h
> index 547094fe8a..2fa57a768e 100644
> --- a/MdeModulePkg/Universal/PCD/Pei/Service.h
> +++ b/MdeModulePkg/Universal/PCD/Pei/Service.h
> @@ -26,7 +26,6 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
>  #include <Library/PcdLib.h>
>  #include <Library/BaseMemoryLib.h>
>  #include <Library/MemoryAllocationLib.h>
> -
>  //
>  // Please make sure the PCD Serivce PEIM Version is consistent with
>  // the version of the generated PEIM PCD Database by build tool.
> @@ -40,6 +39,19 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
>    #error "Please make sure the version of PCD PEIM Service and the
> generated PCD PEI Database match."
>  #endif
> 
> +/**
> +  GuidHob can be used to cache the variable data for the following usage.
> +  This GuidHob is the private GUID hob for PcdPei
> +  One GuidHob will be for one EFI variable.
> +
> +  This structure contains three
> members:VariableGuid,VariableName,VariableBuffer.
> +**/
> +typedef struct  {
> +  IN  CONST EFI_GUID      *VariableGuid;
> +  IN  UINT16              *VariableName;
> +  VOID                    *VariableBuffer;
> +} TEST_DATA;
> +
>  /**
>    Retrieve additional information associated with a PCD token in the default
> token space.
> 
> --
> 2.28.0.windows.1
> 
> 
> 
> 
> 
> 





-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#66781): https://edk2.groups.io/g/devel/message/66781
Mute This Topic: https://groups.io/mt/77902255/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-