[NTOS:Mm] Fail gracefully when no pages are available

This commit is contained in:
Timo Kreuzer 2023-04-02 18:35:30 +03:00
parent d7de53b6d5
commit 3ae12d5a8c
3 changed files with 18 additions and 8 deletions

View file

@ -481,8 +481,11 @@ MiRemoveAnyPage(IN ULONG Color)
/* Make sure PFN lock is held and we have pages */ /* Make sure PFN lock is held and we have pages */
MI_ASSERT_PFN_LOCK_HELD(); MI_ASSERT_PFN_LOCK_HELD();
ASSERT(MmAvailablePages != 0);
ASSERT(Color < MmSecondaryColors); ASSERT(Color < MmSecondaryColors);
if (MmAvailablePages == 0)
{
return 0;
}
/* Check the colored free list */ /* Check the colored free list */
PageIndex = MmFreePagesByColor[FreePageList][Color].Flink; PageIndex = MmFreePagesByColor[FreePageList][Color].Flink;
@ -514,6 +517,7 @@ MiRemoveAnyPage(IN ULONG Color)
/* Remove the page from its list */ /* Remove the page from its list */
PageIndex = MiRemovePageByColor(PageIndex, Color); PageIndex = MiRemovePageByColor(PageIndex, Color);
ASSERT(PageIndex != 0);
/* Sanity checks */ /* Sanity checks */
Pfn1 = MI_PFN_ELEMENT(PageIndex); Pfn1 = MI_PFN_ELEMENT(PageIndex);
@ -538,8 +542,11 @@ MiRemoveZeroPage(IN ULONG Color)
/* Make sure PFN lock is held and we have pages */ /* Make sure PFN lock is held and we have pages */
MI_ASSERT_PFN_LOCK_HELD(); MI_ASSERT_PFN_LOCK_HELD();
ASSERT(MmAvailablePages != 0);
ASSERT(Color < MmSecondaryColors); ASSERT(Color < MmSecondaryColors);
if (MmAvailablePages == 0)
{
return 0;
}
/* Check the colored zero list */ /* Check the colored zero list */
PageIndex = MmFreePagesByColor[ZeroedPageList][Color].Flink; PageIndex = MmFreePagesByColor[ZeroedPageList][Color].Flink;
@ -583,6 +590,7 @@ MiRemoveZeroPage(IN ULONG Color)
/* Remove the page from its list */ /* Remove the page from its list */
PageIndex = MiRemovePageByColor(PageIndex, Color); PageIndex = MiRemovePageByColor(PageIndex, Color);
ASSERT(PageIndex != 0);
ASSERT(Pfn1 == MI_PFN_ELEMENT(PageIndex)); ASSERT(Pfn1 == MI_PFN_ELEMENT(PageIndex));
/* Zero it, if needed */ /* Zero it, if needed */

View file

@ -303,20 +303,21 @@ MmRequestPageMemoryConsumer(ULONG Consumer, BOOLEAN CanWait,
{ {
PFN_NUMBER Page; PFN_NUMBER Page;
/* Update the target */
InterlockedIncrementUL(&MiMemoryConsumers[Consumer].PagesUsed);
UpdateTotalCommittedPages(1);
/* /*
* Actually allocate the page. * Actually allocate the page.
*/ */
Page = MmAllocPage(Consumer); Page = MmAllocPage(Consumer);
if (Page == 0) if (Page == 0)
{ {
KeBugCheck(NO_PAGES_AVAILABLE); *AllocatedPage = 0;
return STATUS_NO_MEMORY;
} }
*AllocatedPage = Page; *AllocatedPage = Page;
/* Update the target */
InterlockedIncrementUL(&MiMemoryConsumers[Consumer].PagesUsed);
UpdateTotalCommittedPages(1);
return(STATUS_SUCCESS); return(STATUS_SUCCESS);
} }

View file

@ -623,7 +623,8 @@ MmAllocPage(ULONG Type)
PfnOffset = MiRemoveZeroPage(MI_GET_NEXT_COLOR()); PfnOffset = MiRemoveZeroPage(MI_GET_NEXT_COLOR());
if (!PfnOffset) if (!PfnOffset)
{ {
KeBugCheck(NO_PAGES_AVAILABLE); MiReleasePfnLock(OldIrql);
return 0;
} }
DPRINT("Legacy allocate: %lx\n", PfnOffset); DPRINT("Legacy allocate: %lx\n", PfnOffset);