From 8daee04c2a2a35fd9eea3c2c9503b8ec59f6b036 Mon Sep 17 00:00:00 2001 From: ReactOS Portable Systems Group Date: Sun, 27 Jul 2008 22:39:54 +0000 Subject: [PATCH] It seems unclear as to what the point of the Page Reference Count Table in the React Addres Space structure really was. It seems an over-engineered approach that actually causes more problems then it attempts to solve. The idea is to be able to unmap PDEs when they are not required anymore (a noble idea), which saves you 4KB of non-paged pool whenever a process frees a chunk of 4MB memory (oversimplification). The problem is that to keep track of this, an extremly expensive series of branches, comparisons, masks and shifts is applied every single time that a page is mapped or unmapped. It also adds 8KB of non-paged pool to keep track of the references, which in some cases can be more wasteful than keeping the page tables around. Finally, if the process quickly allocates and de-allocates memory in the same PDE range, we will effectively map and unmap the PDE continously, fragmenting hyperspace and slowing down perf. This patch removes this functionality from the system and re-uses the code that was already present in Mmi386ReleaseMmInfo (with some optimizations and changes) to do this unmapping when the process exists. This should make things faster, with a very small amount of increased memory footprint (we're talking about less than 100kb of non paged pool, in worse-case scenarios). svn path=/trunk/; revision=34865 --- reactos/ntoskrnl/include/internal/mm.h | 1 - reactos/ntoskrnl/mm/aspace.c | 16 +---- reactos/ntoskrnl/mm/i386/page.c | 92 +------------------------- 3 files changed, 3 insertions(+), 106 deletions(-) diff --git a/reactos/ntoskrnl/include/internal/mm.h b/reactos/ntoskrnl/include/internal/mm.h index 39686a1dc24..cadfff53ed6 100644 --- a/reactos/ntoskrnl/include/internal/mm.h +++ b/reactos/ntoskrnl/include/internal/mm.h @@ -255,7 +255,6 @@ typedef struct _MADDRESS_SPACE PMEMORY_AREA MemoryAreaRoot; PVOID LowestAddress; PEPROCESS Process; - PUSHORT PageTableRefCountTable; PEX_PUSH_LOCK Lock; } MADDRESS_SPACE, *PMADDRESS_SPACE; diff --git a/reactos/ntoskrnl/mm/aspace.c b/reactos/ntoskrnl/mm/aspace.c index dd9cd5e8eaf..7ea175adccd 100644 --- a/reactos/ntoskrnl/mm/aspace.c +++ b/reactos/ntoskrnl/mm/aspace.c @@ -39,8 +39,6 @@ NTAPI MmInitializeAddressSpace(PEPROCESS Process, PMADDRESS_SPACE AddressSpace) { - ULONG Count; - AddressSpace->MemoryAreaRoot = NULL; if (Process != NULL) @@ -48,19 +46,12 @@ MmInitializeAddressSpace(PEPROCESS Process, AddressSpace->LowestAddress = MM_LOWEST_USER_ADDRESS; AddressSpace->Process = Process; AddressSpace->Lock = (PEX_PUSH_LOCK)&Process->AddressCreationLock; - ExInitializePushLock((PULONG_PTR)AddressSpace->Lock); - Count = MiGetUserPageDirectoryCount(); - AddressSpace->PageTableRefCountTable = ExAllocatePoolWithTag(NonPagedPool, - Count * sizeof(USHORT), - TAG_PTRC); - RtlZeroMemory(AddressSpace->PageTableRefCountTable, Count * sizeof(USHORT)); - + ExInitializePushLock((PULONG_PTR)AddressSpace->Lock); } else { AddressSpace->LowestAddress = MmSystemRangeStart; AddressSpace->Process = NULL; - AddressSpace->PageTableRefCountTable = NULL; AddressSpace->Lock = (PEX_PUSH_LOCK)&PsGetCurrentProcess()->AddressCreationLock; ExInitializePushLock((PULONG_PTR)AddressSpace->Lock); } @@ -72,11 +63,6 @@ NTSTATUS NTAPI MmDestroyAddressSpace(PMADDRESS_SPACE AddressSpace) { - if (AddressSpace->PageTableRefCountTable) - { - ExFreePool(AddressSpace->PageTableRefCountTable); - } - return STATUS_SUCCESS; } diff --git a/reactos/ntoskrnl/mm/i386/page.c b/reactos/ntoskrnl/mm/i386/page.c index 01f3ca2a26f..8c99a9aa2cc 100644 --- a/reactos/ntoskrnl/mm/i386/page.c +++ b/reactos/ntoskrnl/mm/i386/page.c @@ -145,9 +145,8 @@ Mmi386ReleaseMmInfo(PEPROCESS Process) { PUSHORT LdtDescriptor; ULONG LdtBase; - PULONG Pde; PULONG PageDir; - ULONG i, j; + ULONG i; DPRINT("Mmi386ReleaseMmInfo(Process %x)\n",Process); @@ -168,28 +167,7 @@ Mmi386ReleaseMmInfo(PEPROCESS Process) { if (PageDir[i] != 0) { - DPRINT1("Pde for %08x - %08x is not freed, RefCount %d\n", - i * 4 * 1024 * 1024, (i + 1) * 4 * 1024 * 1024 - 1, - ((PMADDRESS_SPACE)&Process->VadRoot)->PageTableRefCountTable[i]); - Pde = MmCreateHyperspaceMapping(PTE_TO_PFN(PageDir[i])); - for (j = 0; j < 1024; j++) - { - if(Pde[j] != 0) - { - if (Pde[j] & PA_PRESENT) - { - DPRINT1("Page at %08x is not freed\n", - i * 4 * 1024 * 1024 + j * PAGE_SIZE); - } - else - { - DPRINT1("Swapentry %x at %x is not freed\n", - Pde[j], i * 4 * 1024 * 1024 + j * PAGE_SIZE); - - } - } - } - MmDeleteHyperspaceMapping(Pde); + MiZeroPage(PTE_TO_PFN(PageDir[i])); MmReleasePageMemoryConsumer(MC_NPPOOL, PTE_TO_PFN(PageDir[i])); } } @@ -604,25 +582,6 @@ MmDeleteVirtualMapping(PEPROCESS Process, PVOID Address, BOOLEAN FreePage, { *Page = Pfn; } - /* - * Decrement the reference count for this page table. - */ - if (Process != NULL && WasValid && - ((PMADDRESS_SPACE)&Process->VadRoot)->PageTableRefCountTable != NULL && - Address < MmSystemRangeStart) - { - PUSHORT Ptrc; - ULONG Idx; - - Ptrc = ((PMADDRESS_SPACE)&Process->VadRoot)->PageTableRefCountTable; - Idx = ADDR_TO_PAGE_TABLE(Address); - - Ptrc[Idx]--; - if (Ptrc[Idx] == 0) - { - MmFreePageTable(Process, Address); - } - } } VOID @@ -651,25 +610,6 @@ MmDeletePageFileMapping(PEPROCESS Process, PVOID Address, MiFlushTlb(Pt, Address); - /* - * Decrement the reference count for this page table. - */ - if (Process != NULL && Pte && - ((PMADDRESS_SPACE)&Process->VadRoot)->PageTableRefCountTable != NULL && - Address < MmSystemRangeStart) - { - PUSHORT Ptrc; - - Ptrc = ((PMADDRESS_SPACE)&Process->VadRoot)->PageTableRefCountTable; - - Ptrc[ADDR_TO_PAGE_TABLE(Address)]--; - if (Ptrc[ADDR_TO_PAGE_TABLE(Address)] == 0) - { - MmFreePageTable(Process, Address); - } - } - - /* * Return some information to the caller */ @@ -971,17 +911,6 @@ MmCreatePageFileMapping(PEPROCESS Process, MmUnmapPageTable(Pt); } - if (Process != NULL && - ((PMADDRESS_SPACE)&Process->VadRoot)->PageTableRefCountTable != NULL && - Address < MmSystemRangeStart) - { - PUSHORT Ptrc; - ULONG Idx; - - Ptrc = ((PMADDRESS_SPACE)&Process->VadRoot)->PageTableRefCountTable; - Idx = ADDR_TO_PAGE_TABLE(Address); - Ptrc[Idx]++; - } return(STATUS_SUCCESS); } @@ -1092,16 +1021,6 @@ MmCreateVirtualMappingUnsafe(PEPROCESS Process, MmMarkPageUnmapped(PTE_TO_PFN((Pte))); } (void)InterlockedExchangeUL(Pt, PFN_TO_PTE(Pages[i]) | Attributes); - if (Address < MmSystemRangeStart && - ((PMADDRESS_SPACE)&Process->VadRoot)->PageTableRefCountTable != NULL && - Attributes & PA_PRESENT) - { - PUSHORT Ptrc; - - Ptrc = ((PMADDRESS_SPACE)&Process->VadRoot)->PageTableRefCountTable; - - Ptrc[ADDR_TO_PAGE_TABLE(Addr)]++; - } if (Pte != 0) { if (Address > MmSystemRangeStart || @@ -1411,13 +1330,6 @@ MmInitGlobalKernelPageDirectory(VOID) } } -ULONG -NTAPI -MiGetUserPageDirectoryCount(VOID) -{ - return ADDR_TO_PDE_OFFSET(MmSystemRangeStart); -} - VOID INIT_FUNCTION NTAPI