MdeModulePkg/Core: fix a logic hole in page free
This hole will cause page fault randomly. The root cause is that Guard page, which is just freed back to page pool but not yet cleared not- present attribute, will be allocated right away by internal function CoreFreeMemoryMapStack(). The solution to this issue is to clear the not-present attribute for freed Guard page before doing any free operation, instead of after those operation. The reason we didn't do this before is due to the fact that manipulating page attributes might cause memory allocation action which would cause a dead lock inside a memory allocation/free operation. So we always set or unset Guard page outside the memory lock. After a thorough analysis, we believe clearing a Guard page will not cause memory allocation because memory we're to manipulate was already manipulated before for sure. Therefore there should be no memory allocation occurring in this situation. Since we cleared Guard page not-present attribute before freeing instead of after freeing, the debug code to clear freed memory can now be restored to its original way (aka no checking and bypassing Guard page). Cc: Ruiyu Ni <ruiyu.ni@intel.com> Cc: Eric Dong <eric.dong@intel.com> Cc: Star Zeng <star.zeng@intel.com> Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Jian J Wang <jian.j.wang@intel.com> Reviewed-by: Star Zeng <star.zeng@intel.com> Reviewed-by: Ruiyu Ni <ruiyu.ni@intel.com>
This commit is contained in:
@@ -900,42 +900,17 @@ CoreConvertPagesEx (
|
||||
//
|
||||
CoreAddRange (MemType, Start, RangeEnd, Attribute);
|
||||
if (ChangingType && (MemType == EfiConventionalMemory)) {
|
||||
//
|
||||
// Avoid calling DEBUG_CLEAR_MEMORY() for an address of 0 because this
|
||||
// macro will ASSERT() if address is 0. Instead, CoreAddRange() guarantees
|
||||
// that the page starting at address 0 is always filled with zeros.
|
||||
//
|
||||
if (Start == 0) {
|
||||
//
|
||||
// Avoid calling DEBUG_CLEAR_MEMORY() for an address of 0 because this
|
||||
// macro will ASSERT() if address is 0. Instead, CoreAddRange()
|
||||
// guarantees that the page starting at address 0 is always filled
|
||||
// with zeros.
|
||||
//
|
||||
if (RangeEnd > EFI_PAGE_SIZE) {
|
||||
DEBUG_CLEAR_MEMORY ((VOID *)(UINTN) EFI_PAGE_SIZE, (UINTN) (RangeEnd - EFI_PAGE_SIZE + 1));
|
||||
}
|
||||
} else {
|
||||
//
|
||||
// If Heap Guard is enabled, the page at the top and/or bottom of
|
||||
// this memory block to free might be inaccessible. Skipping them
|
||||
// to avoid page fault exception.
|
||||
//
|
||||
UINT64 StartToClear;
|
||||
UINT64 EndToClear;
|
||||
|
||||
StartToClear = Start;
|
||||
EndToClear = RangeEnd + 1;
|
||||
if (PcdGet8 (PcdHeapGuardPropertyMask) & (BIT1|BIT0)) {
|
||||
if (IsGuardPage(StartToClear)) {
|
||||
StartToClear += EFI_PAGE_SIZE;
|
||||
}
|
||||
if (IsGuardPage (EndToClear - 1)) {
|
||||
EndToClear -= EFI_PAGE_SIZE;
|
||||
}
|
||||
}
|
||||
|
||||
if (EndToClear > StartToClear) {
|
||||
DEBUG_CLEAR_MEMORY(
|
||||
(VOID *)(UINTN)StartToClear,
|
||||
(UINTN)(EndToClear - StartToClear)
|
||||
);
|
||||
}
|
||||
DEBUG_CLEAR_MEMORY ((VOID *)(UINTN) Start, (UINTN) (RangeEnd - Start + 1));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1513,9 +1488,6 @@ CoreInternalFreePages (
|
||||
|
||||
Done:
|
||||
CoreReleaseMemoryLock ();
|
||||
if (IsGuarded) {
|
||||
UnsetGuardForMemory(Memory, NumberOfPages);
|
||||
}
|
||||
return Status;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user