From 353214e42ed3c423782b79183b26b55f72d64e75 Mon Sep 17 00:00:00 2001 From: Hartmut Birr Date: Sun, 6 Jun 2004 08:36:31 +0000 Subject: [PATCH] - Allocate memory for mdl's for paging io from stack instead of the non paged pool. svn path=/trunk/; revision=9631 --- reactos/ntoskrnl/cc/copy.c | 38 ++++++++++++++++++-------- reactos/ntoskrnl/include/internal/mm.h | 4 +-- reactos/ntoskrnl/io/irp.c | 14 ++++++---- reactos/ntoskrnl/mm/anonmem.c | 17 +++--------- reactos/ntoskrnl/mm/section.c | 27 ++++-------------- 5 files changed, 47 insertions(+), 53 deletions(-) diff --git a/reactos/ntoskrnl/cc/copy.c b/reactos/ntoskrnl/cc/copy.c index 2f11e957eb5..e91f47b5086 100644 --- a/reactos/ntoskrnl/cc/copy.c +++ b/reactos/ntoskrnl/cc/copy.c @@ -1,4 +1,4 @@ -/* $Id: copy.c,v 1.22 2004/06/06 07:52:22 hbirr Exp $ +/* $Id: copy.c,v 1.23 2004/06/06 08:36:30 hbirr Exp $ * * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS kernel @@ -33,6 +33,15 @@ static PHYSICAL_ADDRESS CcZeroPage = { 0 }; #endif #define MAX_ZERO_LENGTH (256 * 1024) +#define MAX_RW_LENGTH (64 * 1024) + +#if defined(__GNUC__) +void * alloca(size_t size); +#elif defined(_MSC_VER) +void* _alloca(size_t size); +#else +#error Unknown compiler for alloca intrinsic stack allocation "function" +#endif /* FUNCTIONS *****************************************************************/ @@ -67,6 +76,9 @@ ReadCacheSegmentChain(PBCB Bcb, ULONG ReadOffset, ULONG Length, NTSTATUS Status; ULONG TempLength; KEVENT Event; + PMDL Mdl; + + Mdl = alloca(MmSizeOfMdl(NULL, MAX_RW_LENGTH)); Status = CcRosGetCacheSegmentChain(Bcb, ReadOffset, Length, &head); if (!NT_SUCCESS(Status)) @@ -105,7 +117,6 @@ ReadCacheSegmentChain(PBCB Bcb, ULONG ReadOffset, ULONG Length, { PCACHE_SEGMENT current2; ULONG current_size; - PMDL Mdl; ULONG i; ULONG offset; @@ -124,7 +135,7 @@ ReadCacheSegmentChain(PBCB Bcb, ULONG ReadOffset, ULONG Length, /* * Create an MDL which contains all their pages. */ - Mdl = MmCreateMdl(NULL, NULL, current_size); + MmInitializeMdl(Mdl, NULL, current_size); Mdl->MdlFlags |= (MDL_PAGES_LOCKED | MDL_IO_PAGE_READ); current2 = current; offset = 0; @@ -157,6 +168,7 @@ ReadCacheSegmentChain(PBCB Bcb, ULONG ReadOffset, ULONG Length, KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL); Status = Iosb.Status; } + MmUnmapLockedPages(Mdl->MappedSystemVa, Mdl); if (!NT_SUCCESS(Status) && Status != STATUS_END_OF_FILE) { while (current != NULL) @@ -206,8 +218,10 @@ ReadCacheSegment(PCACHE_SEGMENT CacheSeg) { Size = CacheSeg->Bcb->CacheSegmentSize; } - Mdl = MmCreateMdl(NULL, CacheSeg->BaseAddress, Size); + Mdl = alloca(MmSizeOfMdl(CacheSeg->BaseAddress, Size)); + MmInitializeMdl(Mdl, CacheSeg->BaseAddress, Size); MmBuildMdlForNonPagedPool(Mdl); + Mdl->MdlFlags |= MDL_IO_PAGE_READ; KeInitializeEvent(&Event, NotificationEvent, FALSE); Status = IoPageRead(CacheSeg->Bcb->FileObject, Mdl, &SegOffset, & Event, &IoStatus); if (Status == STATUS_PENDING) @@ -246,8 +260,10 @@ WriteCacheSegment(PCACHE_SEGMENT CacheSeg) { Size = CacheSeg->Bcb->CacheSegmentSize; } - Mdl = MmCreateMdl(NULL, CacheSeg->BaseAddress, Size); + Mdl = alloca(MmSizeOfMdl(CacheSeg->BaseAddress, Size)); + MmInitializeMdl(Mdl, CacheSeg->BaseAddress, Size); MmBuildMdlForNonPagedPool(Mdl); + Mdl->MdlFlags |= MDL_IO_PAGE_READ; KeInitializeEvent(&Event, NotificationEvent, FALSE); Status = IoPageWrite(CacheSeg->Bcb->FileObject, Mdl, &SegOffset, &Event, &IoStatus); if (Status == STATUS_PENDING) @@ -360,7 +376,7 @@ CcCopyRead (IN PFILE_OBJECT FileObject, } while (Length > 0) { - TempLength = min(max(Bcb->CacheSegmentSize, 65536), Length); + TempLength = min(max(Bcb->CacheSegmentSize, MAX_RW_LENGTH), Length); ReadCacheSegmentChain(Bcb, ReadOffset, TempLength, Buffer); ReadLength += TempLength; Length -= TempLength; @@ -533,6 +549,8 @@ CcZeroData (IN PFILE_OBJECT FileObject, if (FileObject->SectionObjectPointer->SharedCacheMap == NULL) { /* File is not cached */ + + Mdl = alloca(MmSizeOfMdl(NULL, MAX_ZERO_LENGTH)); while (Length > 0) { @@ -544,12 +562,7 @@ CcZeroData (IN PFILE_OBJECT FileObject, { CurrentLength = Length; } - Mdl = MmCreateMdl(NULL, (PVOID)WriteOffset.u.LowPart, CurrentLength); - - if (Mdl == NULL) - { - return(FALSE); - } + MmInitializeMdl(Mdl, (PVOID)WriteOffset.u.LowPart, CurrentLength); Mdl->MdlFlags |= (MDL_PAGES_LOCKED | MDL_IO_PAGE_READ); for (i = 0; i < ((Mdl->Size - sizeof(MDL)) / sizeof(ULONG)); i++) { @@ -562,6 +575,7 @@ CcZeroData (IN PFILE_OBJECT FileObject, KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL); Status = Iosb.Status; } + MmUnmapLockedPages(Mdl->MappedSystemVa, Mdl); if (!NT_SUCCESS(Status)) { return(FALSE); diff --git a/reactos/ntoskrnl/include/internal/mm.h b/reactos/ntoskrnl/include/internal/mm.h index 9fb32f2dbf1..8abd58c130d 100644 --- a/reactos/ntoskrnl/include/internal/mm.h +++ b/reactos/ntoskrnl/include/internal/mm.h @@ -368,9 +368,9 @@ extern MM_STATS MmStats; PVOID MmGetDirtyPagesFromWorkingSet(struct _EPROCESS* Process); NTSTATUS -MmWriteToSwapPage(SWAPENTRY SwapEntry, PMDL Mdl); +MmWriteToSwapPage(SWAPENTRY SwapEntry, PHYSICAL_ADDRESS* Page); NTSTATUS -MmReadFromSwapPage(SWAPENTRY SwapEntry, PMDL Mdl); +MmReadFromSwapPage(SWAPENTRY SwapEntry, PHYSICAL_ADDRESS* Page); VOID MmSetFlagsPage(PHYSICAL_ADDRESS PhysicalAddress, ULONG Flags); ULONG diff --git a/reactos/ntoskrnl/io/irp.c b/reactos/ntoskrnl/io/irp.c index 32b9d2c2967..ec80cff8de2 100644 --- a/reactos/ntoskrnl/io/irp.c +++ b/reactos/ntoskrnl/io/irp.c @@ -1,4 +1,4 @@ -/* $Id: irp.c,v 1.60 2004/04/20 23:14:35 gdalsnes Exp $ +/* $Id: irp.c,v 1.61 2004/06/06 08:36:31 hbirr Exp $ * * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS kernel @@ -294,11 +294,15 @@ IofCompleteRequest(PIRP Irp, /* Windows NT File System Internals, page 165 */ if (Irp->Flags & (IRP_PAGING_IO|IRP_CLOSE_OPERATION)) { - if (Irp->Flags & IRP_PAGING_IO) + /* + * If MDL_IO_PAGE_READ is set, then the caller is responsible + * for deallocating of the mdl. + */ + if (Irp->Flags & IRP_PAGING_IO && + Irp->MdlAddress && + !(Irp->MdlAddress->MdlFlags & MDL_IO_PAGE_READ)) { - /* FIXME: - * The mdl should be freed by the caller! - */ + if (Irp->MdlAddress->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA) { MmUnmapLockedPages(Irp->MdlAddress->MappedSystemVa, Irp->MdlAddress); diff --git a/reactos/ntoskrnl/mm/anonmem.c b/reactos/ntoskrnl/mm/anonmem.c index 05b9ce4c144..c869b6c3658 100644 --- a/reactos/ntoskrnl/mm/anonmem.c +++ b/reactos/ntoskrnl/mm/anonmem.c @@ -16,7 +16,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -/* $Id: anonmem.c,v 1.27 2004/04/10 22:35:25 gdalsnes Exp $ +/* $Id: anonmem.c,v 1.28 2004/06/06 08:36:31 hbirr Exp $ * * PROJECT: ReactOS kernel * FILE: ntoskrnl/mm/anonmem.c @@ -46,7 +46,6 @@ MmWritePageVirtualMemory(PMADDRESS_SPACE AddressSpace, { SWAPENTRY SwapEntry; LARGE_INTEGER PhysicalAddress; - PMDL Mdl; NTSTATUS Status; /* @@ -99,9 +98,7 @@ MmWritePageVirtualMemory(PMADDRESS_SPACE AddressSpace, /* * Write the page to the pagefile */ - Mdl = MmCreateMdl(NULL, NULL, PAGE_SIZE); - MmBuildMdlFromPages(Mdl, (PULONG)&PhysicalAddress); - Status = MmWriteToSwapPage(SwapEntry, Mdl); + Status = MmWriteToSwapPage(SwapEntry, &PhysicalAddress); if (!NT_SUCCESS(Status)) { DPRINT1("MM: Failed to write to swap page (Status was 0x%.8X)\n", @@ -133,7 +130,6 @@ MmPageOutVirtualMemory(PMADDRESS_SPACE AddressSpace, BOOL WasDirty; SWAPENTRY SwapEntry; NTSTATUS Status; - PMDL Mdl; DPRINT("MmPageOutVirtualMemory(Address 0x%.8X) PID %d\n", Address, MemoryArea->Process->UniqueProcessId); @@ -200,9 +196,7 @@ MmPageOutVirtualMemory(PMADDRESS_SPACE AddressSpace, /* * Write the page to the pagefile */ - Mdl = MmCreateMdl(NULL, NULL, PAGE_SIZE); - MmBuildMdlFromPages(Mdl, (ULONG *)&PhysicalAddress.u.LowPart); - Status = MmWriteToSwapPage(SwapEntry, Mdl); + Status = MmWriteToSwapPage(SwapEntry, &PhysicalAddress); if (!NT_SUCCESS(Status)) { DPRINT1("MM: Failed to write to swap page (Status was 0x%.8X)\n", @@ -372,12 +366,9 @@ MmNotPresentFaultVirtualMemory(PMADDRESS_SPACE AddressSpace, if (MmIsPageSwapEntry(NULL, Address)) { SWAPENTRY SwapEntry; - PMDL Mdl; MmDeletePageFileMapping(MemoryArea->Process, Address, &SwapEntry); - Mdl = MmCreateMdl(NULL, NULL, PAGE_SIZE); - MmBuildMdlFromPages(Mdl, (PULONG)&Page); - Status = MmReadFromSwapPage(SwapEntry, Mdl); + Status = MmReadFromSwapPage(SwapEntry, &Page); if (!NT_SUCCESS(Status)) { KEBUGCHECK(0); diff --git a/reactos/ntoskrnl/mm/section.c b/reactos/ntoskrnl/mm/section.c index 953a032a015..26c78e26e29 100644 --- a/reactos/ntoskrnl/mm/section.c +++ b/reactos/ntoskrnl/mm/section.c @@ -16,7 +16,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -/* $Id: section.c,v 1.150 2004/05/30 12:55:11 hbirr Exp $ +/* $Id: section.c,v 1.151 2004/06/06 08:36:31 hbirr Exp $ * * PROJECT: ReactOS kernel * FILE: ntoskrnl/mm/section.c @@ -396,10 +396,7 @@ MmUnsharePageEntrySectionSegment(PSECTION_OBJECT Section, * process and the current segment (also not within an other process). */ NTSTATUS Status; - PMDL Mdl; - Mdl = MmCreateMdl(NULL, NULL, PAGE_SIZE); - MmBuildMdlFromPages(Mdl, (PULONG)&Page); - Status = MmWriteToSwapPage(SavedSwapEntry, Mdl); + Status = MmWriteToSwapPage(SavedSwapEntry, &Page); if (!NT_SUCCESS(Status)) { DPRINT1("MM: Failed to write to swap page (Status was 0x%.8X)\n", Status); @@ -792,7 +789,6 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace, * Must be private page we have swapped out. */ SWAPENTRY SwapEntry; - PMDL Mdl; /* * Sanity check @@ -813,9 +809,7 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace, KEBUGCHECK(0); } - Mdl = MmCreateMdl(NULL, NULL, PAGE_SIZE); - MmBuildMdlFromPages(Mdl, (PULONG)&Page); - Status = MmReadFromSwapPage(SwapEntry, Mdl); + Status = MmReadFromSwapPage(SwapEntry, &Page); if (!NT_SUCCESS(Status)) { DPRINT1("MmReadFromSwapPage failed, status = %x\n", Status); @@ -1078,7 +1072,6 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace, else if (IS_SWAP_FROM_SSE(Entry)) { SWAPENTRY SwapEntry; - PMDL Mdl; SwapEntry = SWAPENTRY_FROM_SSE(Entry); @@ -1095,9 +1088,7 @@ MmNotPresentFaultSectionView(PMADDRESS_SPACE AddressSpace, KEBUGCHECK(0); } - Mdl = MmCreateMdl(NULL, NULL, PAGE_SIZE); - MmBuildMdlFromPages(Mdl, (PULONG)&Page); - Status = MmReadFromSwapPage(SwapEntry, Mdl); + Status = MmReadFromSwapPage(SwapEntry, &Page); if (!NT_SUCCESS(Status)) { KEBUGCHECK(0); @@ -1434,7 +1425,6 @@ MmPageOutSectionView(PMADDRESS_SPACE AddressSpace, PHYSICAL_ADDRESS PhysicalAddress; MM_SECTION_PAGEOUT_CONTEXT Context; SWAPENTRY SwapEntry; - PMDL Mdl; ULONG Entry; ULONG FileOffset; NTSTATUS Status; @@ -1695,9 +1685,7 @@ MmPageOutSectionView(PMADDRESS_SPACE AddressSpace, /* * Write the page to the pagefile */ - Mdl = MmCreateMdl(NULL, NULL, PAGE_SIZE); - MmBuildMdlFromPages(Mdl, (PULONG)&PhysicalAddress); - Status = MmWriteToSwapPage(SwapEntry, Mdl); + Status = MmWriteToSwapPage(SwapEntry, &PhysicalAddress); if (!NT_SUCCESS(Status)) { DPRINT1("MM: Failed to write to swap page (Status was 0x%.8X)\n", @@ -1784,7 +1772,6 @@ MmWritePageSectionView(PMADDRESS_SPACE AddressSpace, PMM_SECTION_SEGMENT Segment; PHYSICAL_ADDRESS PhysicalAddress; SWAPENTRY SwapEntry; - PMDL Mdl; ULONG Entry; BOOLEAN Private; NTSTATUS Status; @@ -1900,9 +1887,7 @@ MmWritePageSectionView(PMADDRESS_SPACE AddressSpace, /* * Write the page to the pagefile */ - Mdl = MmCreateMdl(NULL, NULL, PAGE_SIZE); - MmBuildMdlFromPages(Mdl, (PULONG)&PhysicalAddress); - Status = MmWriteToSwapPage(SwapEntry, Mdl); + Status = MmWriteToSwapPage(SwapEntry, &PhysicalAddress); if (!NT_SUCCESS(Status)) { DPRINT1("MM: Failed to write to swap page (Status was 0x%.8X)\n",