Hi Marvin,

1. The memory allocate and free in PiSmmCore are SMRAM that is not related to 
UEFI memory map.
2. According to UEFI 2.6 spec page 222 below, the UEFI OS loader should have 
got the memory map before ExitBootServices.
That means the memory free should not impact the memory map *got by UEFI OS 
loader*, it will only impact the memory map if the UEFI OS loader will call 
GetMemoryMap() again.

"A UEFI OS loader must ensure that it has the system's current memory map at 
the time it calls
ExitBootServices(). This is done by passing in the current memory map's MapKey 
value as
returned by EFI_BOOT_SERVICES.GetMemoryMap()."

Thanks,
Star
-----Original Message-----
From: Marvin Häuser [mailto:marvin.haeu...@outlook.com] 
Sent: Monday, June 20, 2016 1:08 AM
To: edk2-devel@lists.01.org
Cc: Tian, Feng <feng.t...@intel.com>; Zeng, Star <star.z...@intel.com>
Subject: [PATCH v1 1/2] MdeModulePkg: Minimize usage of FreePool() during 
ExitBS().

During exiting Boot Services, there should be no changes made to the Memory 
Map. This patch eliminates calls to FreePool() and
CloseEvent() (which calls FreePool()) and instead zero's the memory.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Marvin Haeuser <marvin.haeu...@outlook.com>
---
 MdeModulePkg/Core/PiSmmCore/PiSmmCore.c             |  2 +-
 MdeModulePkg/Core/PiSmmCore/Smi.c                   | 41 +++++++++++++++++---
 MdeModulePkg/Universal/Network/IScsiDxe/IScsiMisc.c |  6 ++-
 MdeModulePkg/Core/PiSmmCore/PiSmmCore.h             | 17 ++++++++
 4 files changed, 57 insertions(+), 9 deletions(-)

diff --git a/MdeModulePkg/Core/PiSmmCore/PiSmmCore.c 
b/MdeModulePkg/Core/PiSmmCore/PiSmmCore.c
index 7245f201fbdf..96e8da02bbc6 100644
--- a/MdeModulePkg/Core/PiSmmCore/PiSmmCore.c
+++ b/MdeModulePkg/Core/PiSmmCore/PiSmmCore.c
@@ -204,7 +204,7 @@ SmmExitBootServicesHandler (
              NULL
              );
 
-  SmiHandlerUnRegister (DispatchHandle);
+  SmiHandlerUnRegisterWorker (DispatchHandle, TRUE);
 
   return Status;
 }
diff --git a/MdeModulePkg/Core/PiSmmCore/Smi.c 
b/MdeModulePkg/Core/PiSmmCore/Smi.c
index 816d0f519360..a22606517f37 100644
--- a/MdeModulePkg/Core/PiSmmCore/Smi.c
+++ b/MdeModulePkg/Core/PiSmmCore/Smi.c
@@ -280,9 +280,10 @@ SmiHandlerRegister (  }
 
 /**
-  Unregister a handler in SMM.
+  Internal worker function to unregister a handler in SMM.
 
-  @param  DispatchHandle  The handle that was specified when the handler was 
registered.
+  @param  DispatchHandle    The handle that was specified when the handler was 
registered.
+  @param  ExitBootServices  Indicates whether or not ExitBootServices() is in 
progress.
 
   @retval EFI_SUCCESS           Handler function was successfully unregistered.
   @retval EFI_INVALID_PARAMETER DispatchHandle does not refer to a valid 
handle.
@@ -290,8 +291,9 @@ SmiHandlerRegister (  **/  EFI_STATUS  EFIAPI 
-SmiHandlerUnRegister (
-  IN EFI_HANDLE  DispatchHandle
+SmiHandlerUnRegisterWorker (
+  IN  EFI_HANDLE  DispatchHandle,
+  IN  BOOLEAN     ExitBootServices
   )
 {
   SMI_HANDLER  *SmiHandler;
@@ -310,7 +312,12 @@ SmiHandlerUnRegister (
   SmiEntry = SmiHandler->SmiEntry;
 
   RemoveEntryList (&SmiHandler->Link);
-  FreePool (SmiHandler);
+
+  if (!ExitBootServices) {
+    FreePool (SmiHandler);
+  } else {
+    ZeroMem (SmiHandler, sizeof (*SmiHandler));  }
 
   if (SmiEntry == NULL) {
     //
@@ -325,8 +332,30 @@ SmiHandlerUnRegister (
     //
     RemoveEntryList (&SmiEntry->AllEntries);
 
-    FreePool (SmiEntry);
+    if (!ExitBootServices) {
+      FreePool (SmiEntry);
+    } else {
+      ZeroMem (SmiEntry, sizeof (*SmiEntry));
+    }
   }
 
   return EFI_SUCCESS;
 }
+
+/**
+  Unregister a handler in SMM.
+
+  @param  DispatchHandle  The handle that was specified when the handler was 
registered.
+
+  @retval EFI_SUCCESS           Handler function was successfully unregistered.
+  @retval EFI_INVALID_PARAMETER DispatchHandle does not refer to a valid 
handle.
+
+**/
+EFI_STATUS
+EFIAPI
+SmiHandlerUnRegister (
+  IN EFI_HANDLE  DispatchHandle
+  )
+{
+  return SmiHandlerUnRegisterWorker (DispatchHandle, FALSE); }
diff --git a/MdeModulePkg/Universal/Network/IScsiDxe/IScsiMisc.c 
b/MdeModulePkg/Universal/Network/IScsiDxe/IScsiMisc.c
index ae202c3fe24a..a5a7368c8d39 100644
--- a/MdeModulePkg/Universal/Network/IScsiDxe/IScsiMisc.c
+++ b/MdeModulePkg/Universal/Network/IScsiDxe/IScsiMisc.c
@@ -872,9 +872,11 @@ IScsiOnExitBootService (
   ISCSI_DRIVER_DATA *Private;
 
   Private = (ISCSI_DRIVER_DATA *) Context;
-  gBS->CloseEvent (Private->ExitBootServiceEvent);
 
-  IScsiSessionAbort (&Private->Session);
+  if (Private->Session.Signature != 0) {
+    IScsiSessionAbort (&Private->Session);
+    ZeroMem (&Private->Session, sizeof (Private->Session));  }
 }
 
 /**
diff --git a/MdeModulePkg/Core/PiSmmCore/PiSmmCore.h 
b/MdeModulePkg/Core/PiSmmCore/PiSmmCore.h
index 0e9c92abef9a..787786bfba9f 100644
--- a/MdeModulePkg/Core/PiSmmCore/PiSmmCore.h
+++ b/MdeModulePkg/Core/PiSmmCore/PiSmmCore.h
@@ -593,6 +593,23 @@ SmiHandlerRegister (
   );
 
 /**
+  Internal worker function to unregister a handler in SMM.
+
+  @param  DispatchHandle    The handle that was specified when the handler was 
registered.
+  @param  ExitBootServices  Indicates whether or not ExitBootServices() is in 
progress.
+
+  @retval EFI_SUCCESS           Handler function was successfully unregistered.
+  @retval EFI_INVALID_PARAMETER DispatchHandle does not refer to a valid 
handle.
+
+**/
+EFI_STATUS
+EFIAPI
+SmiHandlerUnRegisterWorker (
+  IN  EFI_HANDLE                      DispatchHandle,
+  IN  BOOLEAN                         ExitBootServices
+  );
+
+/**
   Unregister a handler in SMM.
 
   @param  DispatchHandle  The handle that was specified when the handler was 
registered.
-- 
2.9.0.windows.1

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

Reply via email to