Author: fireball Date: Mon Apr 21 16:42:00 2008 New Revision: 33092
URL: http://svn.reactos.org/svn/reactos?rev=33092&view=rev Log: - The address space lock is a pushlock -- pushlocks can fail if callers are acquiring them without first disabling APCs, so they should always be acquired within a critical region. Fix the address space lock to do this. - Don't keep track of the page table reference count table in the address space structure, nobody was using this. - Instead of using either a global kernel pushlock for the kernel address space, and a per-process lock for process address space, always use a per-process lock (the kernel address space uses the idle process' lock). - Instead of acquiring/releasing the lock by dereferencing the process which owns the address space, hold a pointer to the lock directly. This removes an extra dereference, and also allows the kernel lock to work (because kernel address space doesn't have an associated process). - Inline the lock acquisition/release functions and the get-address-space functions. - Measured a 20% performance boost during boot-up, and 150% during installation. - Patch by Alex.
Modified: trunk/reactos/ntoskrnl/include/internal/mm.h trunk/reactos/ntoskrnl/mm/aspace.c
Modified: trunk/reactos/ntoskrnl/include/internal/mm.h URL: http://svn.reactos.org/svn/reactos/trunk/reactos/ntoskrnl/include/internal/m... ============================================================================== --- trunk/reactos/ntoskrnl/include/internal/mm.h [iso-8859-1] (original) +++ trunk/reactos/ntoskrnl/include/internal/mm.h [iso-8859-1] Mon Apr 21 16:42:00 2008 @@ -254,9 +254,9 @@ { PMEMORY_AREA MemoryAreaRoot; PVOID LowestAddress; - struct _EPROCESS* Process; + PEPROCESS Process; PUSHORT PageTableRefCountTable; - ULONG PageTableRefCountTableSize; + PEX_PUSH_LOCK Lock; } MADDRESS_SPACE, *PMADDRESS_SPACE;
typedef struct @@ -394,23 +394,7 @@
VOID NTAPI -MmLockAddressSpace(PMADDRESS_SPACE AddressSpace); - -VOID -NTAPI -MmUnlockAddressSpace(PMADDRESS_SPACE AddressSpace); - -VOID -NTAPI MmInitializeKernelAddressSpace(VOID); - -PMADDRESS_SPACE -NTAPI -MmGetCurrentAddressSpace(VOID); - -PMADDRESS_SPACE -NTAPI -MmGetKernelAddressSpace(VOID);
NTSTATUS NTAPI @@ -1584,4 +1568,37 @@ MmUpdatePageDir((PEPROCESS)Process, Address, Size); }
+ +extern MADDRESS_SPACE MmKernelAddressSpace; + +FORCEINLINE +VOID +MmLockAddressSpace(PMADDRESS_SPACE AddressSpace) +{ + KeEnterCriticalRegion(); + ExAcquirePushLockExclusive(AddressSpace->Lock); +} + +FORCEINLINE +VOID +MmUnlockAddressSpace(PMADDRESS_SPACE AddressSpace) +{ + ExReleasePushLock(AddressSpace->Lock); + KeLeaveCriticalRegion(); +} + +FORCEINLINE +PMADDRESS_SPACE +MmGetCurrentAddressSpace(VOID) +{ + return (PMADDRESS_SPACE)&((PEPROCESS)KeGetCurrentThread()->ApcState.Process)->VadRoot; +} + +FORCEINLINE +PMADDRESS_SPACE +MmGetKernelAddressSpace(VOID) +{ + return &MmKernelAddressSpace; +} + #endif
Modified: trunk/reactos/ntoskrnl/mm/aspace.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/ntoskrnl/mm/aspace.c?rev=33... ============================================================================== --- trunk/reactos/ntoskrnl/mm/aspace.c [iso-8859-1] (original) +++ trunk/reactos/ntoskrnl/mm/aspace.c [iso-8859-1] Mon Apr 21 16:42:00 2008 @@ -19,59 +19,19 @@
/* GLOBALS ******************************************************************/
-static MADDRESS_SPACE KernelAddressSpace; -EX_PUSH_LOCK KernelAddressSpaceLock; +MADDRESS_SPACE MmKernelAddressSpace; + +ULONGLONG Cycles; +ULONG TimeDelta;
/* FUNCTIONS *****************************************************************/ - -VOID -NTAPI -MmLockAddressSpace(PMADDRESS_SPACE AddressSpace) -{ - if (AddressSpace->Process) - { - ExAcquirePushLockExclusive((PEX_PUSH_LOCK)&AddressSpace->Process->AddressCreationLock); - } - else - { - ExAcquirePushLockExclusive(&KernelAddressSpaceLock); - } -} - -VOID -NTAPI -MmUnlockAddressSpace(PMADDRESS_SPACE AddressSpace) -{ - if (AddressSpace->Process) - { - ExReleasePushLock((PEX_PUSH_LOCK)&AddressSpace->Process->AddressCreationLock); - } - else - { - ExReleasePushLock(&KernelAddressSpaceLock); - } -}
VOID INIT_FUNCTION NTAPI MmInitializeKernelAddressSpace(VOID) { - MmInitializeAddressSpace(NULL, &KernelAddressSpace); -} - -PMADDRESS_SPACE -NTAPI -MmGetCurrentAddressSpace(VOID) -{ - return((PMADDRESS_SPACE)&(PsGetCurrentProcess())->VadRoot); -} - -PMADDRESS_SPACE -NTAPI -MmGetKernelAddressSpace(VOID) -{ - return(&KernelAddressSpace); + MmInitializeAddressSpace(NULL, &MmKernelAddressSpace); }
NTSTATUS @@ -79,51 +39,45 @@ MmInitializeAddressSpace(PEPROCESS Process, PMADDRESS_SPACE AddressSpace) { - AddressSpace->MemoryAreaRoot = NULL; - if (Process) - { - ExInitializePushLock((PULONG_PTR)&Process->AddressCreationLock); - } - else - { - ExInitializePushLock((PULONG_PTR)&KernelAddressSpaceLock); - } - if (Process != NULL) - { - AddressSpace->LowestAddress = MM_LOWEST_USER_ADDRESS; - } - else - { - AddressSpace->LowestAddress = MmSystemRangeStart; - } - AddressSpace->Process = Process; - if (Process != NULL) - { - ULONG Count; - Count = MiGetUserPageDirectoryCount(); - AddressSpace->PageTableRefCountTable = - ExAllocatePoolWithTag(NonPagedPool, Count * sizeof(USHORT), - TAG_PTRC); - RtlZeroMemory(AddressSpace->PageTableRefCountTable, Count * sizeof(USHORT)); - AddressSpace->PageTableRefCountTableSize = Count; - } - else - { - AddressSpace->PageTableRefCountTable = NULL; - AddressSpace->PageTableRefCountTableSize = 0; - } - return(STATUS_SUCCESS); + ULONG Count; + + AddressSpace->MemoryAreaRoot = NULL; + + if (Process != NULL) + { + 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)); + + } + else + { + AddressSpace->LowestAddress = MmSystemRangeStart; + AddressSpace->Process = NULL; + AddressSpace->PageTableRefCountTable = NULL; + AddressSpace->Lock = (PEX_PUSH_LOCK)&PsGetCurrentProcess()->AddressCreationLock; + ExInitializePushLock((PULONG_PTR)AddressSpace->Lock); + } + + return STATUS_SUCCESS; }
NTSTATUS NTAPI MmDestroyAddressSpace(PMADDRESS_SPACE AddressSpace) { - if (AddressSpace->PageTableRefCountTable != NULL) - { - ExFreePool(AddressSpace->PageTableRefCountTable); - } - return(STATUS_SUCCESS); + if (AddressSpace->PageTableRefCountTable) + { + ExFreePool(AddressSpace->PageTableRefCountTable); + } + + return STATUS_SUCCESS; }
/* EOF */