]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg/DxeCore: use separate lock for pool allocations
authorArd Biesheuvel <ard.biesheuvel@linaro.org>
Fri, 24 Feb 2017 14:21:18 +0000 (14:21 +0000)
committerArd Biesheuvel <ard.biesheuvel@linaro.org>
Tue, 28 Feb 2017 14:59:50 +0000 (14:59 +0000)
In preparation of adding memory permission attribute management to
the pool allocator, split off the locking of the pool metadata into
a separate lock. This is an improvement in itself, given that pool
allocations can only interfere with the page allocation bookkeeping
if pool pages are allocated or released. But it is also required to
ensure that the permission attribute management does not deadlock,
given that it may trigger page table splits leading to additional
page tables being allocated.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Reviewed-by: Jiewen Yao <jiewen.yao@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
MdeModulePkg/Core/Dxe/Mem/Pool.c

index 7afd2d312c1d45854758df726f143f579ebf63fd..ebb2fceedd8050c7202a20791b8ee278ece4ab91 100644 (file)
@@ -15,6 +15,8 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include "DxeMain.h"\r
 #include "Imem.h"\r
 \r
+STATIC EFI_LOCK mPoolMemoryLock = EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY);\r
+\r
 #define POOL_FREE_SIGNATURE   SIGNATURE_32('p','f','r','0')\r
 typedef struct {\r
   UINT32          Signature;\r
@@ -239,13 +241,13 @@ CoreInternalAllocatePool (
   //\r
   // Acquire the memory lock and make the allocation\r
   //\r
-  Status = CoreAcquireLockOrFail (&gMemoryLock);\r
+  Status = CoreAcquireLockOrFail (&mPoolMemoryLock);\r
   if (EFI_ERROR (Status)) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
   *Buffer = CoreAllocatePoolI (PoolType, Size);\r
-  CoreReleaseMemoryLock ();\r
+  CoreReleaseLock (&mPoolMemoryLock);\r
   return (*Buffer != NULL) ? EFI_SUCCESS : EFI_OUT_OF_RESOURCES;\r
 }\r
 \r
@@ -289,6 +291,28 @@ CoreAllocatePool (
   return Status;\r
 }\r
 \r
+STATIC\r
+VOID *\r
+CoreAllocatePoolPagesI (\r
+  IN EFI_MEMORY_TYPE    PoolType,\r
+  IN UINTN              NoPages,\r
+  IN UINTN              Granularity\r
+  )\r
+{\r
+  VOID        *Buffer;\r
+  EFI_STATUS  Status;\r
+\r
+  Status = CoreAcquireLockOrFail (&gMemoryLock);\r
+  if (EFI_ERROR (Status)) {\r
+    return NULL;\r
+  }\r
+\r
+  Buffer = CoreAllocatePoolPages (PoolType, NoPages, Granularity);\r
+  CoreReleaseMemoryLock ();\r
+\r
+  return Buffer;\r
+}\r
+\r
 /**\r
   Internal function to allocate pool of a particular type.\r
   Caller must have the memory lock held\r
@@ -317,7 +341,7 @@ CoreAllocatePoolI (
   UINTN       NoPages;\r
   UINTN       Granularity;\r
 \r
-  ASSERT_LOCKED (&gMemoryLock);\r
+  ASSERT_LOCKED (&mPoolMemoryLock);\r
 \r
   if  (PoolType == EfiACPIReclaimMemory   ||\r
        PoolType == EfiACPIMemoryNVS       ||\r
@@ -355,7 +379,7 @@ CoreAllocatePoolI (
   if (Index >= SIZE_TO_LIST (Granularity)) {\r
     NoPages = EFI_SIZE_TO_PAGES(Size) + EFI_SIZE_TO_PAGES (Granularity) - 1;\r
     NoPages &= ~(UINTN)(EFI_SIZE_TO_PAGES (Granularity) - 1);\r
-    Head = CoreAllocatePoolPages (PoolType, NoPages, Granularity);\r
+    Head = CoreAllocatePoolPagesI (PoolType, NoPages, Granularity);\r
     goto Done;\r
   }\r
 \r
@@ -383,7 +407,7 @@ CoreAllocatePoolI (
     //\r
     // Get another page\r
     //\r
-    NewPage = CoreAllocatePoolPages(PoolType, EFI_SIZE_TO_PAGES (Granularity), Granularity);\r
+    NewPage = CoreAllocatePoolPages(PoolType, EFI_SIZE_TO_PAGES (Granularity), Granularity);\r
     if (NewPage == NULL) {\r
       goto Done;\r
     }\r
@@ -486,9 +510,9 @@ CoreInternalFreePool (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  CoreAcquireMemoryLock ();\r
+  CoreAcquireLock (&mPoolMemoryLock);\r
   Status = CoreFreePoolI (Buffer, PoolType);\r
-  CoreReleaseMemoryLock ();\r
+  CoreReleaseLock (&mPoolMemoryLock);\r
   return Status;\r
 }\r
 \r
@@ -525,6 +549,19 @@ CoreFreePool (
   return Status;\r
 }\r
 \r
+STATIC\r
+VOID\r
+CoreFreePoolPagesI (\r
+  IN EFI_MEMORY_TYPE        PoolType,\r
+  IN EFI_PHYSICAL_ADDRESS   Memory,\r
+  IN UINTN                  NoPages\r
+  )\r
+{\r
+  CoreAcquireMemoryLock ();\r
+  CoreFreePoolPages (Memory, NoPages);\r
+  CoreReleaseMemoryLock ();\r
+}\r
+\r
 /**\r
   Internal function to free a pool entry.\r
   Caller must have the memory lock held\r
@@ -573,7 +610,7 @@ CoreFreePoolI (
   //\r
   ASSERT (Tail->Signature == POOL_TAIL_SIGNATURE);\r
   ASSERT (Head->Size == Tail->Size);\r
-  ASSERT_LOCKED (&gMemoryLock);\r
+  ASSERT_LOCKED (&mPoolMemoryLock);\r
 \r
   if (Tail->Signature != POOL_TAIL_SIGNATURE) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -624,7 +661,7 @@ CoreFreePoolI (
     //\r
     NoPages = EFI_SIZE_TO_PAGES(Size) + EFI_SIZE_TO_PAGES (Granularity) - 1;\r
     NoPages &= ~(UINTN)(EFI_SIZE_TO_PAGES (Granularity) - 1);\r
-    CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS) (UINTN) Head, NoPages);\r
+    CoreFreePoolPagesI (Pool->MemoryType, (EFI_PHYSICAL_ADDRESS) (UINTN) Head, NoPages);\r
 \r
   } else {\r
 \r
@@ -680,7 +717,8 @@ CoreFreePoolI (
         //\r
         // Free the page\r
         //\r
-        CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS) (UINTN)NewPage, EFI_SIZE_TO_PAGES (Granularity));\r
+        CoreFreePoolPagesI (Pool->MemoryType, (EFI_PHYSICAL_ADDRESS) (UINTN)NewPage,\r
+          EFI_SIZE_TO_PAGES (Granularity));\r
       }\r
     }\r
   }\r