Re: [edk2-devel] [PATCH 9/9] MdeModulePkg:Add global variable mVariableRtCacheInfo

2024-05-17 Thread Ni, Ray
I didn't read through all changes.
2 comments:

  1.
Please do not remember to call ConvertPointer() for any pointers accessed at 
runtime. I think your patch contains 4 pointers in the mVariableRtCacheInfo 
structure.
  2.
Please do not call FreePages() to free a PEI-allocated runtime memory. It won't 
succeed as PEI and DXE memory services do not share one database.
 *
The FreePages() call should be removed in earlier patch when you let the 
Variable driver consume the PEI-allocated memory.

Thanks,
Ray

From: Tan, Dun 
Sent: Friday, May 17, 2024 17:49
To: devel@edk2.groups.io 
Cc: Ni, Ray ; Liming Gao ; Wu, 
Jiaxin 
Subject: [PATCH 9/9] MdeModulePkg:Add global variable mVariableRtCacheInfo

Add global variable mVariableRtCacheInfo to save the
content in gEdkiiVariableRuntimeCacheInfoHobGuid. With
this new global variable, 7 global variables can be
removed.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Liming Gao 
Cc: Jiaxin Wu 
---
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c | 97 
+
 1 file changed, 41 insertions(+), 56 deletions(-)

diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c 
b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c
index 6efe5cee10..de39462d68 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c
@@ -44,26 +44,20 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #include "PrivilegePolymorphic.h"
 #include "VariableParsing.h"

-EFI_HANDLE  mHandle  = NULL;
-EFI_SMM_VARIABLE_PROTOCOL   *mSmmVariable= NULL;
-EFI_EVENT   mVirtualAddressChangeEvent   = NULL;
-EFI_MM_COMMUNICATION2_PROTOCOL  *mMmCommunication2   = NULL;
-UINT8   *mVariableBuffer = NULL;
-UINT8   *mVariableBufferPhysical = NULL;
-VARIABLE_INFO_ENTRY *mVariableInfo   = NULL;
-VARIABLE_STORE_HEADER   *mVariableRuntimeHobCacheBuffer  = NULL;
-VARIABLE_STORE_HEADER   *mVariableRuntimeNvCacheBuffer   = NULL;
-VARIABLE_STORE_HEADER   *mVariableRuntimeVolatileCacheBuffer = NULL;
+EFI_HANDLE  mHandle= NULL;
+EFI_SMM_VARIABLE_PROTOCOL   *mSmmVariable  = NULL;
+EFI_EVENT   mVirtualAddressChangeEvent = NULL;
+EFI_MM_COMMUNICATION2_PROTOCOL  *mMmCommunication2 = NULL;
+UINT8   *mVariableBuffer   = NULL;
+UINT8   *mVariableBufferPhysical   = NULL;
+VARIABLE_INFO_ENTRY *mVariableInfo = NULL;
 UINTN   mVariableBufferSize;
-UINTN   mVariableRuntimeHobCacheBufferSize;
 UINTN   mVariableBufferPayloadSize;
-BOOLEAN *mVariableRuntimeCachePendingUpdate;
-BOOLEAN *mVariableRuntimeCacheReadLock;
 BOOLEAN mVariableAuthFormat;
-BOOLEAN *mHobFlushComplete;
 EFI_LOCKmVariableServicesLock;
 EDKII_VARIABLE_LOCK_PROTOCOLmVariableLock;
 EDKII_VAR_CHECK_PROTOCOLmVarCheck;
+VARIABLE_RUNTIME_CACHE_INFO mVariableRtCacheInfo;

 /**
   The logic to initialize the VariablePolicy engine is in its own file.
@@ -500,21 +494,21 @@ CheckForRuntimeCacheSync (
   VOID
   )
 {
-  if (*mVariableRuntimeCachePendingUpdate) {
+  if (mVariableRtCacheInfo.CacheInfoFlag->PendingUpdate) {
 SyncRuntimeCache ();
   }

-  ASSERT (!(*mVariableRuntimeCachePendingUpdate));
+  ASSERT (!(mVariableRtCacheInfo.CacheInfoFlag->PendingUpdate));

   //
   // The HOB variable data may have finished being flushed in the runtime 
cache sync update
   //
-  if ((*mHobFlushComplete) && (mVariableRuntimeHobCacheBuffer != NULL)) {
+  if ((mVariableRtCacheInfo.CacheInfoFlag->HobFlushComplete) && 
(mVariableRtCacheInfo.RuntimeHobCacheBuffer != 0)) {
 if (!EfiAtRuntime ()) {
-  FreePages (mVariableRuntimeHobCacheBuffer, EFI_SIZE_TO_PAGES 
(mVariableRuntimeHobCacheBufferSize));
+  FreePages ((VOID *)(UINTN)mVariableRtCacheInfo.RuntimeHobCacheBuffer, 
mVariableRtCacheInfo.RuntimeHobCachePages);
 }

-mVariableRuntimeHobCacheBuffer = NULL;
+mVariableRtCacheInfo.RuntimeHobCacheBuffer = 0;
   }
 }

@@ -565,20 +559,20 @@ FindVariableInRuntimeCache (
   // a GetVariable () or GetNextVariable () call from being issued until a 
prior call has returned. The runtime
   // cache read lock should always be free when entering this function.
   //
-  ASSERT (!(*mVariableRuntimeCacheReadLock));
+  ASSERT (!(mVariableRtCacheInfo.CacheInfoFlag->ReadLock));

-  *mVariableRuntime

[edk2-devel] [PATCH 9/9] MdeModulePkg:Add global variable mVariableRtCacheInfo

2024-05-17 Thread duntan
Add global variable mVariableRtCacheInfo to save the
content in gEdkiiVariableRuntimeCacheInfoHobGuid. With
this new global variable, 7 global variables can be
removed.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Liming Gao 
Cc: Jiaxin Wu 
---
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c | 97 
+
 1 file changed, 41 insertions(+), 56 deletions(-)

diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c 
b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c
index 6efe5cee10..de39462d68 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c
@@ -44,26 +44,20 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #include "PrivilegePolymorphic.h"
 #include "VariableParsing.h"
 
-EFI_HANDLE  mHandle  = NULL;
-EFI_SMM_VARIABLE_PROTOCOL   *mSmmVariable= NULL;
-EFI_EVENT   mVirtualAddressChangeEvent   = NULL;
-EFI_MM_COMMUNICATION2_PROTOCOL  *mMmCommunication2   = NULL;
-UINT8   *mVariableBuffer = NULL;
-UINT8   *mVariableBufferPhysical = NULL;
-VARIABLE_INFO_ENTRY *mVariableInfo   = NULL;
-VARIABLE_STORE_HEADER   *mVariableRuntimeHobCacheBuffer  = NULL;
-VARIABLE_STORE_HEADER   *mVariableRuntimeNvCacheBuffer   = NULL;
-VARIABLE_STORE_HEADER   *mVariableRuntimeVolatileCacheBuffer = NULL;
+EFI_HANDLE  mHandle= NULL;
+EFI_SMM_VARIABLE_PROTOCOL   *mSmmVariable  = NULL;
+EFI_EVENT   mVirtualAddressChangeEvent = NULL;
+EFI_MM_COMMUNICATION2_PROTOCOL  *mMmCommunication2 = NULL;
+UINT8   *mVariableBuffer   = NULL;
+UINT8   *mVariableBufferPhysical   = NULL;
+VARIABLE_INFO_ENTRY *mVariableInfo = NULL;
 UINTN   mVariableBufferSize;
-UINTN   mVariableRuntimeHobCacheBufferSize;
 UINTN   mVariableBufferPayloadSize;
-BOOLEAN *mVariableRuntimeCachePendingUpdate;
-BOOLEAN *mVariableRuntimeCacheReadLock;
 BOOLEAN mVariableAuthFormat;
-BOOLEAN *mHobFlushComplete;
 EFI_LOCKmVariableServicesLock;
 EDKII_VARIABLE_LOCK_PROTOCOLmVariableLock;
 EDKII_VAR_CHECK_PROTOCOLmVarCheck;
+VARIABLE_RUNTIME_CACHE_INFO mVariableRtCacheInfo;
 
 /**
   The logic to initialize the VariablePolicy engine is in its own file.
@@ -500,21 +494,21 @@ CheckForRuntimeCacheSync (
   VOID
   )
 {
-  if (*mVariableRuntimeCachePendingUpdate) {
+  if (mVariableRtCacheInfo.CacheInfoFlag->PendingUpdate) {
 SyncRuntimeCache ();
   }
 
-  ASSERT (!(*mVariableRuntimeCachePendingUpdate));
+  ASSERT (!(mVariableRtCacheInfo.CacheInfoFlag->PendingUpdate));
 
   //
   // The HOB variable data may have finished being flushed in the runtime 
cache sync update
   //
-  if ((*mHobFlushComplete) && (mVariableRuntimeHobCacheBuffer != NULL)) {
+  if ((mVariableRtCacheInfo.CacheInfoFlag->HobFlushComplete) && 
(mVariableRtCacheInfo.RuntimeHobCacheBuffer != 0)) {
 if (!EfiAtRuntime ()) {
-  FreePages (mVariableRuntimeHobCacheBuffer, EFI_SIZE_TO_PAGES 
(mVariableRuntimeHobCacheBufferSize));
+  FreePages ((VOID *)(UINTN)mVariableRtCacheInfo.RuntimeHobCacheBuffer, 
mVariableRtCacheInfo.RuntimeHobCachePages);
 }
 
-mVariableRuntimeHobCacheBuffer = NULL;
+mVariableRtCacheInfo.RuntimeHobCacheBuffer = 0;
   }
 }
 
@@ -565,20 +559,20 @@ FindVariableInRuntimeCache (
   // a GetVariable () or GetNextVariable () call from being issued until a 
prior call has returned. The runtime
   // cache read lock should always be free when entering this function.
   //
-  ASSERT (!(*mVariableRuntimeCacheReadLock));
+  ASSERT (!(mVariableRtCacheInfo.CacheInfoFlag->ReadLock));
 
-  *mVariableRuntimeCacheReadLock = TRUE;
+  mVariableRtCacheInfo.CacheInfoFlag->ReadLock = TRUE;
   CheckForRuntimeCacheSync ();
 
-  if (!(*mVariableRuntimeCachePendingUpdate)) {
+  if (!(mVariableRtCacheInfo.CacheInfoFlag->PendingUpdate)) {
 //
 // 0: Volatile, 1: HOB, 2: Non-Volatile.
 // The index and attributes mapping must be kept in this order as 
FindVariable
 // makes use of this mapping to implement search algorithm.
 //
-VariableStoreList[VariableStoreTypeVolatile] = 
mVariableRuntimeVolatileCacheBuffer;
-VariableStoreList[VariableStoreTypeHob]  = 
mVariableRuntimeHobCacheBuffer;
-VariableStoreList[VariableStoreTypeNv]   = 
mVariableRuntimeNvCacheBuffer;
+VariableStoreList[VariableStoreT