]> git.proxmox.com Git - mirror_edk2.git/commitdiff
UefiCpuPkg/CpuDxe: prevent recursive calling of InitializePageTablePool
authorJian J Wang <jian.j.wang@intel.com>
Wed, 24 Oct 2018 03:21:04 +0000 (11:21 +0800)
committerJian J Wang <jian.j.wang@intel.com>
Fri, 26 Oct 2018 02:30:34 +0000 (10:30 +0800)
The freed-memory guard feature will cause a recursive calling
of InitializePageTablePool(). This is due to a fact that
AllocateAlignedPages() is used to allocate page table pool memory.
This function will most likely call gBS->FreePages to free unaligned
pages and then cause another round of page attributes change, like
below (freed pages will be always marked not-present if freed-memory
guard is enabled)

   FreePages() <===============|
=> CpuSetMemoryAttributes()    |
=> <if out of page table>      |
=> InitializePageTablePool()   |
=> AllocateAlignedPages()      |
=> FreePages() ================|

The solution is add a global variable as a lock in page table pool
allocation function and fail any other requests if it has not been
done.

Since this issue will only happen if free-memory guard is enabled,
it won't affect CpuSetMemoryAttributes() in default build of a BIOS.

If free-memory guard is enabled, it only affect the pages
(failed to mark them as not-present) freed in AllocateAlignedPages().

Since those freed pages haven't been used yet (their addresses not
yet exposed to code outside AllocateAlignedPages), it won't compromise
the freed-memory guard feature.

This change will just fail the CpuSetMemoryAttributes() called from
FreePages() but it won't fail the FreePages(). So the error status
won't be propagated upper layer of code.

Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Star Zeng <star.zeng@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Ruiyu Ni <ruiyu.ni@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Jian J Wang <jian.j.wang@intel.com>
Reviewed-by: Eric Dong <eric.dong@intel.com>
UefiCpuPkg/CpuDxe/CpuPageTable.c

index 33e8ee2d2c3ae80f30405c23febfd37ac3ca8f70..4bee8c7772b9bf3ac6342c1a49e66f7dcb95983a 100644 (file)
@@ -100,6 +100,7 @@ PAGE_ATTRIBUTE_TABLE mPageAttributeTable[] = {
 };\r
 \r
 PAGE_TABLE_POOL                   *mPageTablePool = NULL;\r
 };\r
 \r
 PAGE_TABLE_POOL                   *mPageTablePool = NULL;\r
+BOOLEAN                           mPageTablePoolLock = FALSE;\r
 PAGE_TABLE_LIB_PAGING_CONTEXT     mPagingContext;\r
 EFI_SMM_BASE2_PROTOCOL            *mSmmBase2 = NULL;\r
 \r
 PAGE_TABLE_LIB_PAGING_CONTEXT     mPagingContext;\r
 EFI_SMM_BASE2_PROTOCOL            *mSmmBase2 = NULL;\r
 \r
@@ -1046,6 +1047,16 @@ InitializePageTablePool (
   VOID                      *Buffer;\r
   BOOLEAN                   IsModified;\r
 \r
   VOID                      *Buffer;\r
   BOOLEAN                   IsModified;\r
 \r
+  //\r
+  // Do not allow re-entrance.\r
+  //\r
+  if (mPageTablePoolLock) {\r
+    return FALSE;\r
+  }\r
+\r
+  mPageTablePoolLock = TRUE;\r
+  IsModified = FALSE;\r
+\r
   //\r
   // Always reserve at least PAGE_TABLE_POOL_UNIT_PAGES, including one page for\r
   // header.\r
   //\r
   // Always reserve at least PAGE_TABLE_POOL_UNIT_PAGES, including one page for\r
   // header.\r
@@ -1056,9 +1067,15 @@ InitializePageTablePool (
   Buffer = AllocateAlignedPages (PoolPages, PAGE_TABLE_POOL_ALIGNMENT);\r
   if (Buffer == NULL) {\r
     DEBUG ((DEBUG_ERROR, "ERROR: Out of aligned pages\r\n"));\r
   Buffer = AllocateAlignedPages (PoolPages, PAGE_TABLE_POOL_ALIGNMENT);\r
   if (Buffer == NULL) {\r
     DEBUG ((DEBUG_ERROR, "ERROR: Out of aligned pages\r\n"));\r
-    return FALSE;\r
+    goto Done;\r
   }\r
 \r
   }\r
 \r
+  DEBUG ((\r
+    DEBUG_INFO,\r
+    "Paging: added %lu pages to page table pool\r\n",\r
+    (UINT64)PoolPages\r
+    ));\r
+\r
   //\r
   // Link all pools into a list for easier track later.\r
   //\r
   //\r
   // Link all pools into a list for easier track later.\r
   //\r
@@ -1092,7 +1109,9 @@ InitializePageTablePool (
     );\r
   ASSERT (IsModified == TRUE);\r
 \r
     );\r
   ASSERT (IsModified == TRUE);\r
 \r
-  return TRUE;\r
+Done:\r
+  mPageTablePoolLock = FALSE;\r
+  return IsModified;\r
 }\r
 \r
 /**\r
 }\r
 \r
 /**\r