]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/Mem/Pool.c
MdeModulePkg DxeCore: Enhance memory profile for memory leak detection
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Mem / Pool.c
index 23409d35c4284e92e7ffab5d2451ae949e924a57..6ef5fba395f5805794d3972746f61fb365e66c45 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   UEFI Memory pool management functions.\r
 \r
-Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -52,7 +52,7 @@ typedef struct {
 // as we would in a strict power-of-2 sequence\r
 //\r
 STATIC CONST UINT16 mPoolSizeTable[] = {\r
-  64, 128, 192, 320, 512, 832, 1344, 2176, 3520, 5696, 9216, 14912, 24128\r
+  128, 256, 384, 640, 1024, 1664, 2688, 4352, 7040, 11392, 18432, 29824\r
 };\r
 \r
 #define SIZE_TO_LIST(a)   (GetPoolIndexFromSize (a))\r
@@ -85,6 +85,14 @@ POOL            mPoolHead[EfiMaxMemoryType];
 //\r
 LIST_ENTRY      mPoolHeadList = INITIALIZE_LIST_HEAD_VARIABLE (mPoolHeadList);\r
 \r
+/**\r
+  Get pool size table index from the specified size.\r
+\r
+  @param  Size          The specified size to get index from pool table.\r
+\r
+  @return               The index of pool size table.\r
+\r
+**/\r
 STATIC\r
 UINTN\r
 GetPoolIndexFromSize (\r
@@ -146,10 +154,11 @@ LookupPoolHead (
   }\r
 \r
   //\r
-  // MemoryType values in the range 0x80000000..0xFFFFFFFF are reserved for use by UEFI \r
-  // OS loaders that are provided by operating system vendors\r
+  // MemoryType values in the range 0x80000000..0xFFFFFFFF are reserved for use by UEFI\r
+  // OS loaders that are provided by operating system vendors.\r
+  // MemoryType values in the range 0x70000000..0x7FFFFFFF are reserved for OEM use.\r
   //\r
-  if ((INT32)MemoryType < 0) {\r
+  if ((UINT32) MemoryType >= MEMORY_TYPE_OEM_RESERVED_MIN) {\r
 \r
     for (Link = mPoolHeadList.ForwardLink; Link != &mPoolHeadList; Link = Link->ForwardLink) {\r
       Pool = CR(Link, POOL, Link, POOL_SIGNATURE);\r
@@ -188,7 +197,9 @@ LookupPoolHead (
   @param  Buffer                 The address to return a pointer to the allocated\r
                                  pool\r
 \r
-  @retval EFI_INVALID_PARAMETER  PoolType not valid or Buffer is NULL. \r
+  @retval EFI_INVALID_PARAMETER  Buffer is NULL.\r
+                                 PoolType is in the range EfiMaxMemoryType..0x6FFFFFFF.\r
+                                 PoolType is EfiPersistentMemory.\r
   @retval EFI_OUT_OF_RESOURCES   Size exceeds max pool size or allocation failed.\r
   @retval EFI_SUCCESS            Pool successfully allocated.\r
 \r
@@ -206,8 +217,8 @@ CoreInternalAllocatePool (
   //\r
   // If it's not a valid type, fail it\r
   //\r
-  if ((PoolType >= EfiMaxMemoryType && PoolType <= 0x7fffffff) ||\r
-       PoolType == EfiConventionalMemory) {\r
+  if ((PoolType >= EfiMaxMemoryType && PoolType < MEMORY_TYPE_OEM_RESERVED_MIN) ||\r
+       (PoolType == EfiConventionalMemory) || (PoolType == EfiPersistentMemory)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
@@ -246,7 +257,9 @@ CoreInternalAllocatePool (
   @param  Buffer                 The address to return a pointer to the allocated\r
                                  pool\r
 \r
-  @retval EFI_INVALID_PARAMETER  PoolType not valid or Buffer is NULL. \r
+  @retval EFI_INVALID_PARAMETER  Buffer is NULL.\r
+                                 PoolType is in the range EfiMaxMemoryType..0x6FFFFFFF.\r
+                                 PoolType is EfiPersistentMemory.\r
   @retval EFI_OUT_OF_RESOURCES   Size exceeds max pool size or allocation failed.\r
   @retval EFI_SUCCESS            Pool successfully allocated.\r
 \r
@@ -263,7 +276,15 @@ CoreAllocatePool (
 \r
   Status = CoreInternalAllocatePool (PoolType, Size, Buffer);\r
   if (!EFI_ERROR (Status)) {\r
-    CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0), MemoryProfileActionAllocatePool, PoolType, Size, *Buffer);\r
+    CoreUpdateProfile (\r
+      (EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0),\r
+      MemoryProfileActionAllocatePool,\r
+      PoolType,\r
+      Size,\r
+      *Buffer,\r
+      NULL\r
+      );\r
+    InstallMemoryAttributesTableOnMemoryAllocation (PoolType);\r
   }\r
   return Status;\r
 }\r
@@ -292,7 +313,7 @@ CoreAllocatePoolI (
   VOID        *Buffer;\r
   UINTN       Index;\r
   UINTN       FSize;\r
-  UINTN       Offset;\r
+  UINTN       Offset, MaxOffset;\r
   UINTN       NoPages;\r
   UINTN       Granularity;\r
 \r
@@ -343,6 +364,22 @@ CoreAllocatePoolI (
   //\r
   if (IsListEmpty (&Pool->FreeList[Index])) {\r
 \r
+    Offset = LIST_TO_SIZE (Index);\r
+    MaxOffset = Granularity;\r
+\r
+    //\r
+    // Check the bins holding larger blocks, and carve one up if needed\r
+    //\r
+    while (++Index < SIZE_TO_LIST (Granularity)) {\r
+      if (!IsListEmpty (&Pool->FreeList[Index])) {\r
+        Free = CR (Pool->FreeList[Index].ForwardLink, POOL_FREE, Link, POOL_FREE_SIGNATURE);\r
+        RemoveEntryList (&Free->Link);\r
+        NewPage = (VOID *) Free;\r
+        MaxOffset = LIST_TO_SIZE (Index);\r
+        goto Carve;\r
+      }\r
+    }\r
+\r
     //\r
     // Get another page\r
     //\r
@@ -354,29 +391,28 @@ CoreAllocatePoolI (
     //\r
     // Serve the allocation request from the head of the allocated block\r
     //\r
+Carve:\r
     Head = (POOL_HEAD *) NewPage;\r
-    Offset = LIST_TO_SIZE (Index);\r
 \r
     //\r
     // Carve up remaining space into free pool blocks\r
     //\r
-    Index = SIZE_TO_LIST (Granularity) - 1;\r
-    while (Offset < Granularity) {\r
+    Index--;\r
+    while (Offset < MaxOffset) {\r
       ASSERT (Index < MAX_POOL_LIST);\r
       FSize = LIST_TO_SIZE(Index);\r
 \r
-      while (Offset + FSize <= Granularity) {\r
+      while (Offset + FSize <= MaxOffset) {\r
         Free = (POOL_FREE *) &NewPage[Offset];\r
         Free->Signature = POOL_FREE_SIGNATURE;\r
         Free->Index     = (UINT32)Index;\r
         InsertHeadList (&Pool->FreeList[Index], &Free->Link);\r
         Offset += FSize;\r
       }\r
-\r
       Index -= 1;\r
     }\r
 \r
-    ASSERT (Offset == Granularity);\r
+    ASSERT (Offset == MaxOffset);\r
     goto Done;\r
   }\r
 \r
@@ -431,6 +467,7 @@ Done:
   Frees pool.\r
 \r
   @param  Buffer                 The allocated pool entry to free\r
+  @param  PoolType               Pointer to pool type\r
 \r
   @retval EFI_INVALID_PARAMETER  Buffer is not a valid value.\r
   @retval EFI_SUCCESS            Pool successfully freed.\r
@@ -439,7 +476,8 @@ Done:
 EFI_STATUS\r
 EFIAPI\r
 CoreInternalFreePool (\r
-  IN VOID        *Buffer\r
+  IN VOID               *Buffer,\r
+  OUT EFI_MEMORY_TYPE   *PoolType OPTIONAL\r
   )\r
 {\r
   EFI_STATUS Status;\r
@@ -449,7 +487,7 @@ CoreInternalFreePool (
   }\r
 \r
   CoreAcquireMemoryLock ();\r
-  Status = CoreFreePoolI (Buffer);\r
+  Status = CoreFreePoolI (Buffer, PoolType);\r
   CoreReleaseMemoryLock ();\r
   return Status;\r
 }\r
@@ -469,11 +507,20 @@ CoreFreePool (
   IN VOID  *Buffer\r
   )\r
 {\r
-  EFI_STATUS  Status;\r
+  EFI_STATUS        Status;\r
+  EFI_MEMORY_TYPE   PoolType;\r
 \r
-  Status = CoreInternalFreePool (Buffer);\r
+  Status = CoreInternalFreePool (Buffer, &PoolType);\r
   if (!EFI_ERROR (Status)) {\r
-    CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0), MemoryProfileActionFreePool, (EFI_MEMORY_TYPE) 0, 0, Buffer);\r
+    CoreUpdateProfile (\r
+      (EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0),\r
+      MemoryProfileActionFreePool,\r
+      PoolType,\r
+      0,\r
+      Buffer,\r
+      NULL\r
+      );\r
+    InstallMemoryAttributesTableOnMemoryAllocation (PoolType);\r
   }\r
   return Status;\r
 }\r
@@ -483,6 +530,7 @@ CoreFreePool (
   Caller must have the memory lock held\r
 \r
   @param  Buffer                 The allocated pool entry to free\r
+  @param  PoolType               Pointer to pool type\r
 \r
   @retval EFI_INVALID_PARAMETER  Buffer not valid\r
   @retval EFI_SUCCESS            Buffer successfully freed.\r
@@ -490,7 +538,8 @@ CoreFreePool (
 **/\r
 EFI_STATUS\r
 CoreFreePoolI (\r
-  IN VOID       *Buffer\r
+  IN VOID               *Buffer,\r
+  OUT EFI_MEMORY_TYPE   *PoolType OPTIONAL\r
   )\r
 {\r
   POOL        *Pool;\r
@@ -555,6 +604,10 @@ CoreFreePoolI (
     Granularity = DEFAULT_PAGE_ALLOCATION;\r
   }\r
 \r
+  if (PoolType != NULL) {\r
+    *PoolType = Head->Type;\r
+  }\r
+\r
   //\r
   // Determine the pool list\r
   //\r
@@ -633,13 +686,13 @@ CoreFreePoolI (
   }\r
 \r
   //\r
-  // If this is an OS specific memory type, then check to see if the last\r
+  // If this is an OS/OEM specific memory type, then check to see if the last\r
   // portion of that memory type has been freed.  If it has, then free the\r
   // list entry for that memory type\r
   //\r
-  if ((INT32)Pool->MemoryType < 0 && Pool->Used == 0) {\r
+  if (((UINT32) Pool->MemoryType >= MEMORY_TYPE_OEM_RESERVED_MIN) && Pool->Used == 0) {\r
     RemoveEntryList (&Pool->Link);\r
-    CoreFreePoolI (Pool);\r
+    CoreFreePoolI (Pool, NULL);\r
   }\r
 \r
   return EFI_SUCCESS;\r