2008-03-09 14:11:42 +00:00
|
|
|
/*
|
2005-01-26 13:58:37 +00:00
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
2005-05-09 01:38:29 +00:00
|
|
|
* PROJECT: ReactOS kernel
|
2005-01-26 13:58:37 +00:00
|
|
|
* FILE: ntoskrnl/mm/balance.c
|
|
|
|
* PURPOSE: kernel memory managment functions
|
2005-05-09 01:38:29 +00:00
|
|
|
*
|
2005-01-26 13:58:37 +00:00
|
|
|
* PROGRAMMERS: David Welch (welch@cwcom.net)
|
2011-11-29 23:59:25 +00:00
|
|
|
* Cameron Gutman (cameron.gutman@reactos.org)
|
2001-12-28 00:04:45 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES *****************************************************************/
|
|
|
|
|
2004-08-15 16:39:12 +00:00
|
|
|
#include <ntoskrnl.h>
|
2001-12-28 00:04:45 +00:00
|
|
|
#define NDEBUG
|
2008-08-30 16:31:06 +00:00
|
|
|
#include <debug.h>
|
2001-12-28 00:04:45 +00:00
|
|
|
|
2012-02-20 20:51:18 +00:00
|
|
|
#include "ARM3/miarm.h"
|
|
|
|
|
2005-11-28 23:25:31 +00:00
|
|
|
#if defined (ALLOC_PRAGMA)
|
|
|
|
#pragma alloc_text(INIT, MmInitializeBalancer)
|
|
|
|
#pragma alloc_text(INIT, MmInitializeMemoryConsumer)
|
|
|
|
#pragma alloc_text(INIT, MiInitBalancerThread)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2001-12-28 00:04:45 +00:00
|
|
|
/* TYPES ********************************************************************/
|
2001-12-31 01:53:46 +00:00
|
|
|
typedef struct _MM_ALLOCATION_REQUEST
|
|
|
|
{
|
2014-10-05 06:33:34 +00:00
|
|
|
PFN_NUMBER Page;
|
|
|
|
LIST_ENTRY ListEntry;
|
|
|
|
KEVENT Event;
|
2004-04-10 22:36:07 +00:00
|
|
|
}
|
|
|
|
MM_ALLOCATION_REQUEST, *PMM_ALLOCATION_REQUEST;
|
2001-12-28 00:04:45 +00:00
|
|
|
/* GLOBALS ******************************************************************/
|
|
|
|
|
2004-04-22 01:57:49 +00:00
|
|
|
MM_MEMORY_CONSUMER MiMemoryConsumers[MC_MAXIMUM];
|
2001-12-28 00:04:45 +00:00
|
|
|
static ULONG MiMinimumAvailablePages;
|
|
|
|
static ULONG MiNrTotalPages;
|
2001-12-31 01:53:46 +00:00
|
|
|
static LIST_ENTRY AllocationListHead;
|
|
|
|
static KSPIN_LOCK AllocationListLock;
|
2011-11-29 23:59:25 +00:00
|
|
|
static ULONG MiMinimumPagesPerRun;
|
2001-12-28 00:04:45 +00:00
|
|
|
|
2003-11-16 15:20:39 +00:00
|
|
|
static CLIENT_ID MiBalancerThreadId;
|
|
|
|
static HANDLE MiBalancerThreadHandle = NULL;
|
|
|
|
static KEVENT MiBalancerEvent;
|
|
|
|
static KTIMER MiBalancerTimer;
|
|
|
|
|
2001-12-28 00:04:45 +00:00
|
|
|
/* FUNCTIONS ****************************************************************/
|
|
|
|
|
2005-09-14 01:05:50 +00:00
|
|
|
VOID
|
|
|
|
INIT_FUNCTION
|
|
|
|
NTAPI
|
2003-10-23 20:28:08 +00:00
|
|
|
MmInitializeBalancer(ULONG NrAvailablePages, ULONG NrSystemPages)
|
2001-12-28 00:04:45 +00:00
|
|
|
{
|
2014-10-05 06:33:34 +00:00
|
|
|
memset(MiMemoryConsumers, 0, sizeof(MiMemoryConsumers));
|
|
|
|
InitializeListHead(&AllocationListHead);
|
|
|
|
KeInitializeSpinLock(&AllocationListLock);
|
2004-04-10 22:36:07 +00:00
|
|
|
|
2014-10-05 06:33:34 +00:00
|
|
|
MiNrTotalPages = NrAvailablePages;
|
2004-04-10 22:36:07 +00:00
|
|
|
|
2014-10-05 06:33:34 +00:00
|
|
|
/* Set up targets. */
|
2016-10-18 20:19:00 +00:00
|
|
|
MiMinimumAvailablePages = 256;
|
2014-10-05 06:33:34 +00:00
|
|
|
MiMinimumPagesPerRun = 256;
|
2009-02-18 16:56:42 +00:00
|
|
|
if ((NrAvailablePages + NrSystemPages) >= 8192)
|
|
|
|
{
|
[HAL/NDK]
- Make Vector parameter in HalEnableSystemInterrupt, HalDisableSystemInterrupt and HalBeginSystemInterrupt an ULONG, not an UCHAR
[NDK]
- 64bit fixes for HANDLE_TABLE, KPROCESS, SECTION_IMAGE_INFORMATION, MMADDRESS_LIST, MMVAD_FLAGS, MMVAD, MMVAD_LONG, MMVAD_SHORT, MEMORY_DESCRIPTOR, MEMORY_ALLOCATION_DESCRIPTOR, LdrVerifyMappedImageMatchesChecksum
- KDPC_DATA::DpcQueueDepth is signed on amd64, unsigned on x86
[NTOSKRNL]
- Fix hundreds of MSVC and amd64 warnings
- add a pragma message to FstubFixupEfiPartition, since it looks broken
- Move portable Ke constants from <arch>/cpu.c to krnlinit.c
- Fixed a bug in amd64 KiGeneralProtectionFaultHandler
svn path=/trunk/; revision=53734
2011-09-18 13:11:45 +00:00
|
|
|
MiMemoryConsumers[MC_CACHE].PagesTarget = NrAvailablePages / 4 * 3;
|
2009-02-18 16:56:42 +00:00
|
|
|
}
|
|
|
|
else if ((NrAvailablePages + NrSystemPages) >= 4096)
|
|
|
|
{
|
|
|
|
MiMemoryConsumers[MC_CACHE].PagesTarget = NrAvailablePages / 3 * 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
[HAL/NDK]
- Make Vector parameter in HalEnableSystemInterrupt, HalDisableSystemInterrupt and HalBeginSystemInterrupt an ULONG, not an UCHAR
[NDK]
- 64bit fixes for HANDLE_TABLE, KPROCESS, SECTION_IMAGE_INFORMATION, MMADDRESS_LIST, MMVAD_FLAGS, MMVAD, MMVAD_LONG, MMVAD_SHORT, MEMORY_DESCRIPTOR, MEMORY_ALLOCATION_DESCRIPTOR, LdrVerifyMappedImageMatchesChecksum
- KDPC_DATA::DpcQueueDepth is signed on amd64, unsigned on x86
[NTOSKRNL]
- Fix hundreds of MSVC and amd64 warnings
- add a pragma message to FstubFixupEfiPartition, since it looks broken
- Move portable Ke constants from <arch>/cpu.c to krnlinit.c
- Fixed a bug in amd64 KiGeneralProtectionFaultHandler
svn path=/trunk/; revision=53734
2011-09-18 13:11:45 +00:00
|
|
|
MiMemoryConsumers[MC_CACHE].PagesTarget = NrAvailablePages / 8;
|
2009-02-18 16:56:42 +00:00
|
|
|
}
|
2014-10-05 06:33:34 +00:00
|
|
|
MiMemoryConsumers[MC_USER].PagesTarget = NrAvailablePages - MiMinimumAvailablePages;
|
2001-12-28 00:04:45 +00:00
|
|
|
}
|
|
|
|
|
2005-09-14 01:05:50 +00:00
|
|
|
VOID
|
|
|
|
INIT_FUNCTION
|
|
|
|
NTAPI
|
2014-10-05 06:33:34 +00:00
|
|
|
MmInitializeMemoryConsumer(
|
|
|
|
ULONG Consumer,
|
|
|
|
NTSTATUS (*Trim)(ULONG Target, ULONG Priority, PULONG NrFreed))
|
2001-12-28 00:04:45 +00:00
|
|
|
{
|
2014-10-05 06:33:34 +00:00
|
|
|
MiMemoryConsumers[Consumer].Trim = Trim;
|
2001-12-28 00:04:45 +00:00
|
|
|
}
|
|
|
|
|
2010-09-27 21:58:54 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
MiZeroPhysicalPage(
|
|
|
|
IN PFN_NUMBER PageFrameIndex
|
|
|
|
);
|
|
|
|
|
2001-12-28 00:04:45 +00:00
|
|
|
NTSTATUS
|
2005-09-14 01:05:50 +00:00
|
|
|
NTAPI
|
2010-07-15 22:50:12 +00:00
|
|
|
MmReleasePageMemoryConsumer(ULONG Consumer, PFN_NUMBER Page)
|
2001-12-28 00:04:45 +00:00
|
|
|
{
|
2014-10-05 06:33:34 +00:00
|
|
|
if (Page == 0)
|
|
|
|
{
|
|
|
|
DPRINT1("Tried to release page zero.\n");
|
|
|
|
KeBugCheck(MEMORY_MANAGEMENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MmGetReferenceCountPage(Page) == 1)
|
|
|
|
{
|
|
|
|
if(Consumer == MC_USER) MmRemoveLRUUserPage(Page);
|
|
|
|
(void)InterlockedDecrementUL(&MiMemoryConsumers[Consumer].PagesUsed);
|
|
|
|
}
|
|
|
|
|
2016-10-18 20:19:00 +00:00
|
|
|
MmDereferencePage(Page);
|
|
|
|
|
2014-10-05 06:33:34 +00:00
|
|
|
return(STATUS_SUCCESS);
|
2001-12-28 00:04:45 +00:00
|
|
|
}
|
|
|
|
|
2011-12-28 01:18:35 +00:00
|
|
|
ULONG
|
2005-09-14 01:05:50 +00:00
|
|
|
NTAPI
|
2011-12-28 01:18:35 +00:00
|
|
|
MiTrimMemoryConsumer(ULONG Consumer, ULONG InitialTarget)
|
2001-12-28 00:04:45 +00:00
|
|
|
{
|
2012-02-22 21:18:56 +00:00
|
|
|
ULONG Target = InitialTarget;
|
2011-11-29 23:59:25 +00:00
|
|
|
ULONG NrFreedPages = 0;
|
|
|
|
NTSTATUS Status;
|
2001-12-28 00:04:45 +00:00
|
|
|
|
2011-11-29 23:59:25 +00:00
|
|
|
/* Make sure we can trim this consumer */
|
|
|
|
if (!MiMemoryConsumers[Consumer].Trim)
|
2011-12-28 01:18:35 +00:00
|
|
|
{
|
|
|
|
/* Return the unmodified initial target */
|
|
|
|
return InitialTarget;
|
|
|
|
}
|
2001-12-28 00:04:45 +00:00
|
|
|
|
2011-11-29 23:59:25 +00:00
|
|
|
if (MiMemoryConsumers[Consumer].PagesUsed > MiMemoryConsumers[Consumer].PagesTarget)
|
|
|
|
{
|
|
|
|
/* Consumer page limit exceeded */
|
|
|
|
Target = max(Target, MiMemoryConsumers[Consumer].PagesUsed - MiMemoryConsumers[Consumer].PagesTarget);
|
|
|
|
}
|
|
|
|
if (MmAvailablePages < MiMinimumAvailablePages)
|
|
|
|
{
|
|
|
|
/* Global page limit exceeded */
|
2012-03-28 19:41:40 +00:00
|
|
|
Target = (ULONG)max(Target, MiMinimumAvailablePages - MmAvailablePages);
|
2011-11-29 23:59:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Target)
|
|
|
|
{
|
2011-12-28 01:18:35 +00:00
|
|
|
if (!InitialTarget)
|
|
|
|
{
|
|
|
|
/* If there was no initial target,
|
|
|
|
* swap at least MiMinimumPagesPerRun */
|
|
|
|
Target = max(Target, MiMinimumPagesPerRun);
|
|
|
|
}
|
2011-11-29 23:59:25 +00:00
|
|
|
|
|
|
|
/* Now swap the pages out */
|
|
|
|
Status = MiMemoryConsumers[Consumer].Trim(Target, 0, &NrFreedPages);
|
|
|
|
|
2013-08-31 16:02:13 +00:00
|
|
|
DPRINT("Trimming consumer %lu: Freed %lu pages with a target of %lu pages\n", Consumer, NrFreedPages, Target);
|
2011-12-10 04:11:19 +00:00
|
|
|
|
2011-11-29 23:59:25 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
KeBugCheck(MEMORY_MANAGEMENT);
|
|
|
|
}
|
2011-12-28 01:18:35 +00:00
|
|
|
|
|
|
|
/* Update the target */
|
|
|
|
if (NrFreedPages < Target)
|
|
|
|
Target -= NrFreedPages;
|
|
|
|
else
|
|
|
|
Target = 0;
|
|
|
|
|
|
|
|
/* Return the remaining pages needed to meet the target */
|
|
|
|
return Target;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Initial target is zero and we don't have anything else to add */
|
|
|
|
return 0;
|
2011-11-29 23:59:25 +00:00
|
|
|
}
|
2001-12-28 00:04:45 +00:00
|
|
|
}
|
|
|
|
|
- Create mmsup.c in ReactOS memory manager directory. It hosts misc support functions:
- Copy kmap.c here, since it's the very definition of "misc support function"
- Copy some exported functions in mm.c which were listed as "misc functions"
- Warn that current implementation of MmIsNonPagedSystemAddressValid will kill kittens.
- Rename mm.c to mmfault.c, since other than the misc functions now in mmsup.c, it was all routines to handle page/access faults.
- Warn that MmIsAddressValid, as currently implemented, kills puppies.
- Move WriteWatch functions to virtual.c since they're part of the Virtual API system call set already hosted there.
- Move the global variables that people had been throwing in here to mminit.c, which is slightly more appropriate.
- Move wset.c's MmTrimUserMemory to balance.c, since that's where all other similar functions are located.
- Incidentally, kill wset.c, as this was the only function present.
- No functional changes, just refactoring and cleanup (other than warning the critter murder the two broken functions will achieve if called).
svn path=/trunk/; revision=41659
2009-06-28 07:52:30 +00:00
|
|
|
NTSTATUS
|
|
|
|
MmTrimUserMemory(ULONG Target, ULONG Priority, PULONG NrFreedPages)
|
|
|
|
{
|
2010-07-15 22:50:12 +00:00
|
|
|
PFN_NUMBER CurrentPage;
|
|
|
|
PFN_NUMBER NextPage;
|
- Create mmsup.c in ReactOS memory manager directory. It hosts misc support functions:
- Copy kmap.c here, since it's the very definition of "misc support function"
- Copy some exported functions in mm.c which were listed as "misc functions"
- Warn that current implementation of MmIsNonPagedSystemAddressValid will kill kittens.
- Rename mm.c to mmfault.c, since other than the misc functions now in mmsup.c, it was all routines to handle page/access faults.
- Warn that MmIsAddressValid, as currently implemented, kills puppies.
- Move WriteWatch functions to virtual.c since they're part of the Virtual API system call set already hosted there.
- Move the global variables that people had been throwing in here to mminit.c, which is slightly more appropriate.
- Move wset.c's MmTrimUserMemory to balance.c, since that's where all other similar functions are located.
- Incidentally, kill wset.c, as this was the only function present.
- No functional changes, just refactoring and cleanup (other than warning the critter murder the two broken functions will achieve if called).
svn path=/trunk/; revision=41659
2009-06-28 07:52:30 +00:00
|
|
|
NTSTATUS Status;
|
2011-11-29 08:13:56 +00:00
|
|
|
|
- Create mmsup.c in ReactOS memory manager directory. It hosts misc support functions:
- Copy kmap.c here, since it's the very definition of "misc support function"
- Copy some exported functions in mm.c which were listed as "misc functions"
- Warn that current implementation of MmIsNonPagedSystemAddressValid will kill kittens.
- Rename mm.c to mmfault.c, since other than the misc functions now in mmsup.c, it was all routines to handle page/access faults.
- Warn that MmIsAddressValid, as currently implemented, kills puppies.
- Move WriteWatch functions to virtual.c since they're part of the Virtual API system call set already hosted there.
- Move the global variables that people had been throwing in here to mminit.c, which is slightly more appropriate.
- Move wset.c's MmTrimUserMemory to balance.c, since that's where all other similar functions are located.
- Incidentally, kill wset.c, as this was the only function present.
- No functional changes, just refactoring and cleanup (other than warning the critter murder the two broken functions will achieve if called).
svn path=/trunk/; revision=41659
2009-06-28 07:52:30 +00:00
|
|
|
(*NrFreedPages) = 0;
|
2011-11-29 08:13:56 +00:00
|
|
|
|
- Create mmsup.c in ReactOS memory manager directory. It hosts misc support functions:
- Copy kmap.c here, since it's the very definition of "misc support function"
- Copy some exported functions in mm.c which were listed as "misc functions"
- Warn that current implementation of MmIsNonPagedSystemAddressValid will kill kittens.
- Rename mm.c to mmfault.c, since other than the misc functions now in mmsup.c, it was all routines to handle page/access faults.
- Warn that MmIsAddressValid, as currently implemented, kills puppies.
- Move WriteWatch functions to virtual.c since they're part of the Virtual API system call set already hosted there.
- Move the global variables that people had been throwing in here to mminit.c, which is slightly more appropriate.
- Move wset.c's MmTrimUserMemory to balance.c, since that's where all other similar functions are located.
- Incidentally, kill wset.c, as this was the only function present.
- No functional changes, just refactoring and cleanup (other than warning the critter murder the two broken functions will achieve if called).
svn path=/trunk/; revision=41659
2009-06-28 07:52:30 +00:00
|
|
|
CurrentPage = MmGetLRUFirstUserPage();
|
|
|
|
while (CurrentPage != 0 && Target > 0)
|
|
|
|
{
|
|
|
|
Status = MmPageOutPhysicalAddress(CurrentPage);
|
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
DPRINT("Succeeded\n");
|
|
|
|
Target--;
|
|
|
|
(*NrFreedPages)++;
|
|
|
|
}
|
2011-11-29 08:13:56 +00:00
|
|
|
|
|
|
|
NextPage = MmGetLRUNextUserPage(CurrentPage);
|
|
|
|
if (NextPage <= CurrentPage)
|
|
|
|
{
|
|
|
|
/* We wrapped around, so we're done */
|
|
|
|
break;
|
|
|
|
}
|
- Create mmsup.c in ReactOS memory manager directory. It hosts misc support functions:
- Copy kmap.c here, since it's the very definition of "misc support function"
- Copy some exported functions in mm.c which were listed as "misc functions"
- Warn that current implementation of MmIsNonPagedSystemAddressValid will kill kittens.
- Rename mm.c to mmfault.c, since other than the misc functions now in mmsup.c, it was all routines to handle page/access faults.
- Warn that MmIsAddressValid, as currently implemented, kills puppies.
- Move WriteWatch functions to virtual.c since they're part of the Virtual API system call set already hosted there.
- Move the global variables that people had been throwing in here to mminit.c, which is slightly more appropriate.
- Move wset.c's MmTrimUserMemory to balance.c, since that's where all other similar functions are located.
- Incidentally, kill wset.c, as this was the only function present.
- No functional changes, just refactoring and cleanup (other than warning the critter murder the two broken functions will achieve if called).
svn path=/trunk/; revision=41659
2009-06-28 07:52:30 +00:00
|
|
|
CurrentPage = NextPage;
|
|
|
|
}
|
2011-11-29 08:13:56 +00:00
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
- Create mmsup.c in ReactOS memory manager directory. It hosts misc support functions:
- Copy kmap.c here, since it's the very definition of "misc support function"
- Copy some exported functions in mm.c which were listed as "misc functions"
- Warn that current implementation of MmIsNonPagedSystemAddressValid will kill kittens.
- Rename mm.c to mmfault.c, since other than the misc functions now in mmsup.c, it was all routines to handle page/access faults.
- Warn that MmIsAddressValid, as currently implemented, kills puppies.
- Move WriteWatch functions to virtual.c since they're part of the Virtual API system call set already hosted there.
- Move the global variables that people had been throwing in here to mminit.c, which is slightly more appropriate.
- Move wset.c's MmTrimUserMemory to balance.c, since that's where all other similar functions are located.
- Incidentally, kill wset.c, as this was the only function present.
- No functional changes, just refactoring and cleanup (other than warning the critter murder the two broken functions will achieve if called).
svn path=/trunk/; revision=41659
2009-06-28 07:52:30 +00:00
|
|
|
}
|
|
|
|
|
2003-11-16 15:20:39 +00:00
|
|
|
static BOOLEAN
|
|
|
|
MiIsBalancerThread(VOID)
|
|
|
|
{
|
2014-10-05 06:33:34 +00:00
|
|
|
return (MiBalancerThreadHandle != NULL) &&
|
|
|
|
(PsGetCurrentThreadId() == MiBalancerThreadId.UniqueThread);
|
2003-11-16 15:20:39 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:59:25 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
MmRebalanceMemoryConsumers(VOID)
|
|
|
|
{
|
|
|
|
if (MiBalancerThreadHandle != NULL &&
|
|
|
|
!MiIsBalancerThread())
|
|
|
|
{
|
|
|
|
KeSetEvent(&MiBalancerEvent, IO_NO_INCREMENT, FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-28 00:04:45 +00:00
|
|
|
NTSTATUS
|
2005-09-14 01:05:50 +00:00
|
|
|
NTAPI
|
2004-04-10 22:36:07 +00:00
|
|
|
MmRequestPageMemoryConsumer(ULONG Consumer, BOOLEAN CanWait,
|
2010-07-15 22:50:12 +00:00
|
|
|
PPFN_NUMBER AllocatedPage)
|
2001-12-28 00:04:45 +00:00
|
|
|
{
|
2014-10-05 06:33:34 +00:00
|
|
|
ULONG PagesUsed;
|
|
|
|
PFN_NUMBER Page;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure we don't exceed our individual target.
|
|
|
|
*/
|
|
|
|
PagesUsed = InterlockedIncrementUL(&MiMemoryConsumers[Consumer].PagesUsed);
|
|
|
|
if (PagesUsed > MiMemoryConsumers[Consumer].PagesTarget &&
|
|
|
|
!MiIsBalancerThread())
|
|
|
|
{
|
|
|
|
MmRebalanceMemoryConsumers();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate always memory for the non paged pool and for the pager thread.
|
|
|
|
*/
|
2016-10-18 20:19:00 +00:00
|
|
|
if ((Consumer == MC_SYSTEM) /* || MiIsBalancerThread() */)
|
2014-10-05 06:33:34 +00:00
|
|
|
{
|
|
|
|
Page = MmAllocPage(Consumer);
|
|
|
|
if (Page == 0)
|
|
|
|
{
|
|
|
|
KeBugCheck(NO_PAGES_AVAILABLE);
|
|
|
|
}
|
|
|
|
if (Consumer == MC_USER) MmInsertLRULastUserPage(Page);
|
|
|
|
*AllocatedPage = Page;
|
|
|
|
if (MmAvailablePages < MiMinimumAvailablePages)
|
|
|
|
MmRebalanceMemoryConsumers();
|
|
|
|
return(STATUS_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure we don't exceed global targets.
|
|
|
|
*/
|
2016-10-18 20:19:00 +00:00
|
|
|
if (((MmAvailablePages < MiMinimumAvailablePages) && !MiIsBalancerThread())
|
|
|
|
|| (MmAvailablePages < (MiMinimumAvailablePages / 2)))
|
2014-10-05 06:33:34 +00:00
|
|
|
{
|
|
|
|
MM_ALLOCATION_REQUEST Request;
|
|
|
|
|
|
|
|
if (!CanWait)
|
|
|
|
{
|
|
|
|
(void)InterlockedDecrementUL(&MiMemoryConsumers[Consumer].PagesUsed);
|
|
|
|
MmRebalanceMemoryConsumers();
|
|
|
|
return(STATUS_NO_MEMORY);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Insert an allocation request. */
|
|
|
|
Request.Page = 0;
|
|
|
|
KeInitializeEvent(&Request.Event, NotificationEvent, FALSE);
|
|
|
|
|
|
|
|
ExInterlockedInsertTailList(&AllocationListHead, &Request.ListEntry, &AllocationListLock);
|
|
|
|
MmRebalanceMemoryConsumers();
|
|
|
|
|
|
|
|
KeWaitForSingleObject(&Request.Event,
|
|
|
|
0,
|
|
|
|
KernelMode,
|
|
|
|
FALSE,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
Page = Request.Page;
|
|
|
|
if (Page == 0)
|
|
|
|
{
|
|
|
|
KeBugCheck(NO_PAGES_AVAILABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(Consumer == MC_USER) MmInsertLRULastUserPage(Page);
|
|
|
|
*AllocatedPage = Page;
|
|
|
|
|
|
|
|
if (MmAvailablePages < MiMinimumAvailablePages)
|
|
|
|
{
|
|
|
|
MmRebalanceMemoryConsumers();
|
|
|
|
}
|
|
|
|
|
|
|
|
return(STATUS_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Actually allocate the page.
|
|
|
|
*/
|
|
|
|
Page = MmAllocPage(Consumer);
|
|
|
|
if (Page == 0)
|
|
|
|
{
|
|
|
|
KeBugCheck(NO_PAGES_AVAILABLE);
|
|
|
|
}
|
|
|
|
if(Consumer == MC_USER) MmInsertLRULastUserPage(Page);
|
|
|
|
*AllocatedPage = Page;
|
|
|
|
|
|
|
|
if (MmAvailablePages < MiMinimumAvailablePages)
|
|
|
|
{
|
|
|
|
MmRebalanceMemoryConsumers();
|
|
|
|
}
|
|
|
|
|
|
|
|
return(STATUS_SUCCESS);
|
2001-12-28 00:04:45 +00:00
|
|
|
}
|
2003-07-10 21:05:04 +00:00
|
|
|
|
2014-08-06 21:53:09 +00:00
|
|
|
|
2008-11-29 20:47:48 +00:00
|
|
|
VOID NTAPI
|
2003-11-16 15:20:39 +00:00
|
|
|
MiBalancerThread(PVOID Unused)
|
|
|
|
{
|
2014-10-05 06:33:34 +00:00
|
|
|
PVOID WaitObjects[2];
|
|
|
|
NTSTATUS Status;
|
|
|
|
ULONG i;
|
2011-12-19 08:49:42 +00:00
|
|
|
|
2014-10-05 06:33:34 +00:00
|
|
|
WaitObjects[0] = &MiBalancerEvent;
|
|
|
|
WaitObjects[1] = &MiBalancerTimer;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
Status = KeWaitForMultipleObjects(2,
|
|
|
|
WaitObjects,
|
|
|
|
WaitAny,
|
|
|
|
Executive,
|
|
|
|
KernelMode,
|
|
|
|
FALSE,
|
|
|
|
NULL,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
if (Status == STATUS_WAIT_0 || Status == STATUS_WAIT_1)
|
2012-03-05 19:40:44 +00:00
|
|
|
{
|
2014-10-05 06:33:34 +00:00
|
|
|
ULONG InitialTarget = 0;
|
|
|
|
|
|
|
|
#if (_MI_PAGING_LEVELS == 2)
|
|
|
|
if (!MiIsBalancerThread())
|
2012-03-05 19:40:44 +00:00
|
|
|
{
|
2014-10-05 06:33:34 +00:00
|
|
|
/* Clean up the unused PDEs */
|
|
|
|
ULONG_PTR Address;
|
|
|
|
PEPROCESS Process = PsGetCurrentProcess();
|
|
|
|
|
|
|
|
/* Acquire PFN lock */
|
2017-11-21 22:33:42 +00:00
|
|
|
KIRQL OldIrql = MiAcquirePfnLock();
|
2014-10-05 06:33:34 +00:00
|
|
|
PMMPDE pointerPde;
|
|
|
|
for (Address = (ULONG_PTR)MI_LOWEST_VAD_ADDRESS;
|
|
|
|
Address < (ULONG_PTR)MM_HIGHEST_VAD_ADDRESS;
|
|
|
|
Address += (PAGE_SIZE * PTE_COUNT))
|
2012-03-05 19:40:44 +00:00
|
|
|
{
|
2014-10-05 06:33:34 +00:00
|
|
|
if (MiQueryPageTableReferences((PVOID)Address) == 0)
|
|
|
|
{
|
|
|
|
pointerPde = MiAddressToPde(Address);
|
|
|
|
if (pointerPde->u.Hard.Valid)
|
|
|
|
MiDeletePte(pointerPde, MiPdeToPte(pointerPde), Process, NULL);
|
|
|
|
ASSERT(pointerPde->u.Hard.Valid == 0);
|
|
|
|
}
|
2012-03-05 19:40:44 +00:00
|
|
|
}
|
2014-10-05 06:33:34 +00:00
|
|
|
/* Release lock */
|
2017-11-21 22:33:42 +00:00
|
|
|
MiReleasePfnLock(OldIrql);
|
2012-03-05 19:40:44 +00:00
|
|
|
}
|
|
|
|
#endif
|
2014-10-05 06:33:34 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
ULONG OldTarget = InitialTarget;
|
|
|
|
|
|
|
|
/* Trim each consumer */
|
|
|
|
for (i = 0; i < MC_MAXIMUM; i++)
|
|
|
|
{
|
|
|
|
InitialTarget = MiTrimMemoryConsumer(i, InitialTarget);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No pages left to swap! */
|
|
|
|
if (InitialTarget != 0 &&
|
|
|
|
InitialTarget == OldTarget)
|
|
|
|
{
|
|
|
|
/* Game over */
|
|
|
|
KeBugCheck(NO_PAGES_AVAILABLE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (InitialTarget != 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DPRINT1("KeWaitForMultipleObjects failed, status = %x\n", Status);
|
|
|
|
KeBugCheck(MEMORY_MANAGEMENT);
|
|
|
|
}
|
|
|
|
}
|
2003-11-16 15:20:39 +00:00
|
|
|
}
|
|
|
|
|
2016-10-18 20:19:00 +00:00
|
|
|
BOOLEAN MmRosNotifyAvailablePage(PFN_NUMBER Page)
|
|
|
|
{
|
|
|
|
PLIST_ENTRY Entry;
|
|
|
|
PMM_ALLOCATION_REQUEST Request;
|
|
|
|
PMMPFN Pfn1;
|
|
|
|
|
|
|
|
/* Make sure the PFN lock is held */
|
2017-11-21 22:33:42 +00:00
|
|
|
MI_ASSERT_PFN_LOCK_HELD();
|
2016-10-18 20:19:00 +00:00
|
|
|
|
|
|
|
if (!MiMinimumAvailablePages)
|
|
|
|
{
|
|
|
|
/* Dirty way to know if we were initialized. */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
Entry = ExInterlockedRemoveHeadList(&AllocationListHead, &AllocationListLock);
|
|
|
|
if (!Entry)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
Request = CONTAINING_RECORD(Entry, MM_ALLOCATION_REQUEST, ListEntry);
|
|
|
|
MiZeroPhysicalPage(Page);
|
|
|
|
Request->Page = Page;
|
|
|
|
|
|
|
|
Pfn1 = MiGetPfnEntry(Page);
|
|
|
|
ASSERT(Pfn1->u3.e2.ReferenceCount == 0);
|
|
|
|
Pfn1->u3.e2.ReferenceCount = 1;
|
|
|
|
Pfn1->u3.e1.PageLocation = ActiveAndValid;
|
|
|
|
|
|
|
|
/* This marks the PFN as a ReactOS PFN */
|
|
|
|
Pfn1->u4.AweAllocation = TRUE;
|
|
|
|
|
|
|
|
/* Allocate the extra ReactOS Data and zero it out */
|
|
|
|
Pfn1->u1.SwapEntry = 0;
|
|
|
|
Pfn1->RmapListHead = NULL;
|
|
|
|
|
|
|
|
KeSetEvent(&Request->Event, IO_NO_INCREMENT, FALSE);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2005-09-14 01:05:50 +00:00
|
|
|
VOID
|
|
|
|
INIT_FUNCTION
|
|
|
|
NTAPI
|
2003-11-16 15:20:39 +00:00
|
|
|
MiInitBalancerThread(VOID)
|
|
|
|
{
|
2014-10-05 06:33:34 +00:00
|
|
|
KPRIORITY Priority;
|
|
|
|
NTSTATUS Status;
|
2003-12-30 18:52:06 +00:00
|
|
|
#if !defined(__GNUC__)
|
2004-04-10 22:36:07 +00:00
|
|
|
|
2014-10-05 06:33:34 +00:00
|
|
|
LARGE_INTEGER dummyJunkNeeded;
|
|
|
|
dummyJunkNeeded.QuadPart = -20000000; /* 2 sec */
|
|
|
|
;
|
2003-12-30 18:52:06 +00:00
|
|
|
#endif
|
2003-11-16 15:20:39 +00:00
|
|
|
|
|
|
|
|
2014-10-05 06:33:34 +00:00
|
|
|
KeInitializeEvent(&MiBalancerEvent, SynchronizationEvent, FALSE);
|
|
|
|
KeInitializeTimerEx(&MiBalancerTimer, SynchronizationTimer);
|
|
|
|
KeSetTimerEx(&MiBalancerTimer,
|
2003-12-30 18:52:06 +00:00
|
|
|
#if defined(__GNUC__)
|
2014-10-05 06:33:34 +00:00
|
|
|
(LARGE_INTEGER)(LONGLONG)-20000000LL, /* 2 sec */
|
2003-12-30 18:52:06 +00:00
|
|
|
#else
|
2014-10-05 06:33:34 +00:00
|
|
|
dummyJunkNeeded,
|
2003-12-30 18:52:06 +00:00
|
|
|
#endif
|
2014-10-05 06:33:34 +00:00
|
|
|
2000, /* 2 sec */
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
Status = PsCreateSystemThread(&MiBalancerThreadHandle,
|
|
|
|
THREAD_ALL_ACCESS,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&MiBalancerThreadId,
|
|
|
|
MiBalancerThread,
|
|
|
|
NULL);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
KeBugCheck(MEMORY_MANAGEMENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
Priority = LOW_REALTIME_PRIORITY + 1;
|
|
|
|
NtSetInformationThread(MiBalancerThreadHandle,
|
|
|
|
ThreadPriority,
|
|
|
|
&Priority,
|
|
|
|
sizeof(Priority));
|
2004-04-10 22:36:07 +00:00
|
|
|
|
2003-11-16 15:20:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-10 21:05:04 +00:00
|
|
|
/* EOF */
|