Skip to content

Commit

Permalink
Decouple page file alloc/free from image sections.
Browse files Browse the repository at this point in the history
This change exposes page file space allocation and free routines to the
rest of the kernel and converts them to fill in IMAGE_BACKING structures
rather than IMAGE_SECTION structures. This is in preparation for I/O to
allocate and free page file space to back shared memory objects.
  • Loading branch information
ccstevens committed Oct 4, 2016
1 parent b7d2bfa commit 7e9fa2b
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 128 deletions.
82 changes: 82 additions & 0 deletions include/minoca/kernel/mm.h
Original file line number Diff line number Diff line change
Expand Up @@ -3295,6 +3295,88 @@ Return Value:
--*/

KSTATUS
MmAllocatePageFileSpace (
PIMAGE_BACKING ImageBacking,
UINTN Size
);

/*++
Routine Description:
This routine allocates space from a page file.
Arguments:
ImageBacking - Supplies a pointer to an image backing structure that
recevies the allocated page file space.
Size - Supplies the size of the page file space to allocate, in bytes.
Return Value:
STATUS_SUCCESS on success. In this case the image backing structure
parameterwill be filled out.
STATUS_INSUFFICIENT_RESOURCES if the request could not be satisified.
--*/

VOID
MmFreePageFileSpace (
PIMAGE_BACKING ImageBacking,
UINTN Size
);

/*++
Routine Description:
This routine frees space from a page file.
Arguments:
ImageBacking - Supplies a pointer to the page file image backing to release.
Size - Supplies the size of the image backing.
Return Value:
None.
--*/

VOID
MmFreePartialPageFileSpace (
PIMAGE_BACKING ImageBacking,
UINTN PageOffset,
UINTN PageCount
);

/*++
Routine Description:
This routine frees a portion of the original space allocated in the page
file.
Arguments:
ImageBacking - Supplies a pointer to the image backing taking up page file
space.
PageOffset - Supplies the offset in pages to the beginning of the region
that should be freed.
PageCount - Supplies the number of pages to free.
Return Value:
None.
--*/

KSTATUS
MmMapFileSection (
HANDLE FileHandle,
Expand Down
13 changes: 11 additions & 2 deletions kernel/mm/imgsec.c
Original file line number Diff line number Diff line change
Expand Up @@ -3154,7 +3154,12 @@ Return Value:

HolePageCount = ((UINTN)HoleEnd - (UINTN)HoleBegin) >> PageShift;
MmpUnmapImageSection(Section, HolePageOffset, HolePageCount, 0, NULL);
MmpFreePartialPageFileSpace(Section, HolePageOffset, HolePageCount);

ASSERT(HolePageOffset + HolePageCount <= (Section->Size >> PageShift));

MmFreePartialPageFileSpace(&(Section->PageFileBacking),
HolePageOffset,
HolePageCount);
}

//
Expand Down Expand Up @@ -3467,7 +3472,11 @@ Return Value:
ASSERT(ImageSection->AddressListEntry.Next == NULL);
ASSERT(ImageSection->ImageListEntry.Next == NULL);

MmpFreePageFileSpace(ImageSection);
MmFreePageFileSpace(&(ImageSection->PageFileBacking), ImageSection->Size);
if (ImageSection->PagingInIrp != NULL) {
IoDestroyIrp(ImageSection->PagingInIrp);
}

if (ImageSection->Lock != NULL) {
KeDestroyQueuedLock(ImageSection->Lock);
}
Expand Down
79 changes: 0 additions & 79 deletions kernel/mm/mmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -2093,85 +2093,6 @@ Return Value:
--*/

KSTATUS
MmpAllocatePageFileSpace (
PIMAGE_SECTION Section
);

/*++
Routine Description:
This routine allocates space from a page file. It is assumed the image
section lock is already held.
Arguments:
Section - Supplies a pointer to the image section. It is expected that the
page count member will be filled out. On successful return, the page
file backing member will be filled out with the allocation.
Return Value:
STATUS_SUCCESS on success. In this case the page file backing member of the
section parameter will be filled out.
STATUS_INSUFFICIENT_RESOURCES if the request could not be satisified.
--*/

VOID
MmpFreePageFileSpace (
PIMAGE_SECTION Section
);

/*++
Routine Description:
This routine frees space from a page file.
Arguments:
Section - Supplies a pointer to the memory section taking up page file
space.
Return Value:
None.
--*/

VOID
MmpFreePartialPageFileSpace (
PIMAGE_SECTION Section,
UINTN PageOffset,
UINTN PageCount
);

/*++
Routine Description:
This routine frees a portion of the original space allocated by an image
section in the page file.
Arguments:
Section - Supplies a pointer to the memory section taking up page file
space.
PageOffset - Supplies the offset in pages to the beginning of the region
that should be freed.
PageCount - Supplies the number of pages to free.
Return Value:
None.
--*/

KSTATUS
MmpPageIn (
PIMAGE_SECTION ImageSection,
Expand Down
89 changes: 42 additions & 47 deletions kernel/mm/paging.c
Original file line number Diff line number Diff line change
Expand Up @@ -743,27 +743,28 @@ Return Value:
}

KSTATUS
MmpAllocatePageFileSpace (
PIMAGE_SECTION Section
MmAllocatePageFileSpace (
PIMAGE_BACKING ImageBacking,
UINTN Size
)

/*++
Routine Description:
This routine allocates space from a page file. It is assumed the image
section lock is already held.
This routine allocates space from a page file.
Arguments:
Section - Supplies a pointer to the image section. It is expected that the
page count member will be filled out. On successful return, the page
file backing member will be filled out with the allocation.
ImageBacking - Supplies a pointer to an image backing structure that
recevies the allocated page file space.
Size - Supplies the size of the page file space to allocate, in bytes.
Return Value:
STATUS_SUCCESS on success. In this case the page file backing member of the
section parameter will be filled out.
STATUS_SUCCESS on success. In this case the image backing structure
parameterwill be filled out.
STATUS_INSUFFICIENT_RESOURCES if the request could not be satisified.
Expand All @@ -780,7 +781,7 @@ Return Value:
KSTATUS Status;

ASSERT(KeGetRunLevel() == RunLevelLow);
ASSERT(Section->PageFileBacking.DeviceHandle == INVALID_HANDLE);
ASSERT(ImageBacking->DeviceHandle == INVALID_HANDLE);

//
// If paging is not enabled, then there's no way page file space could be
Expand All @@ -798,10 +799,9 @@ Return Value:
PageShift = MmPageShift();
PageSize = MmPageSize();

ASSERT((IS_ALIGNED((UINTN)(Section->VirtualAddress), PageSize)) &&
(IS_ALIGNED(Section->Size, PageSize)));
ASSERT(IS_ALIGNED(Size, PageSize));

PageCount = Section->Size >> PageShift;
PageCount = Size >> PageShift;

//
// Look through all page files for the given space.
Expand Down Expand Up @@ -830,8 +830,8 @@ Return Value:
//

if (KSUCCESS(Status)) {
Section->PageFileBacking.DeviceHandle = CurrentPageFile;
Section->PageFileBacking.Offset = Allocation << PageShift;
ImageBacking->DeviceHandle = CurrentPageFile;
ImageBacking->Offset = Allocation << PageShift;
break;
}
}
Expand All @@ -843,9 +843,9 @@ Return Value:

AllocatePageFileSpaceEnd:
if (!KSUCCESS(Status)) {
if (Section->PageFileBacking.DeviceHandle != INVALID_HANDLE) {
MmpFreeFromPageFile(Section->PageFileBacking.DeviceHandle,
Section->PageFileBacking.Offset >> PageShift,
if (ImageBacking->DeviceHandle != INVALID_HANDLE) {
MmpFreeFromPageFile(ImageBacking->DeviceHandle,
ImageBacking->Offset >> PageShift,
PageCount);
}
}
Expand All @@ -854,8 +854,9 @@ Return Value:
}

VOID
MmpFreePageFileSpace (
PIMAGE_SECTION Section
MmFreePageFileSpace (
PIMAGE_BACKING ImageBacking,
UINTN Size
)

/*++
Expand All @@ -866,8 +867,9 @@ Routine Description:
Arguments:
Section - Supplies a pointer to the memory section taking up page file
space.
ImageBacking - Supplies a pointer to the page file image backing to release.
Size - Supplies the size of the image backing.
Return Value:
Expand All @@ -886,26 +888,22 @@ Return Value:
return;
}

if (Section->PagingInIrp != NULL) {
IoDestroyIrp(Section->PagingInIrp);
}

if (Section->PageFileBacking.DeviceHandle == INVALID_HANDLE) {
if (ImageBacking->DeviceHandle == INVALID_HANDLE) {
return;
}

PageShift = MmPageShift();
PageCount = Section->Size >> PageShift;
MmpFreeFromPageFile(Section->PageFileBacking.DeviceHandle,
Section->PageFileBacking.Offset >> PageShift,
PageCount = Size >> PageShift;
MmpFreeFromPageFile(ImageBacking->DeviceHandle,
ImageBacking->Offset >> PageShift,
PageCount);

return;
}

VOID
MmpFreePartialPageFileSpace (
PIMAGE_SECTION Section,
MmFreePartialPageFileSpace (
PIMAGE_BACKING ImageBacking,
UINTN PageOffset,
UINTN PageCount
)
Expand All @@ -914,12 +912,12 @@ MmpFreePartialPageFileSpace (
Routine Description:
This routine frees a portion of the original space allocated by an image
section in the page file.
This routine frees a portion of the original space allocated in the page
file.
Arguments:
Section - Supplies a pointer to the memory section taking up page file
ImageBacking - Supplies a pointer to the image backing taking up page file
space.
PageOffset - Supplies the offset in pages to the beginning of the region
Expand All @@ -944,21 +942,13 @@ Return Value:
return;
}

if (Section->PageFileBacking.DeviceHandle == INVALID_HANDLE) {
if (ImageBacking->DeviceHandle == INVALID_HANDLE) {
return;
}

PageShift = MmPageShift();

ASSERT(PageOffset + PageCount <= (Section->Size >> PageShift));

PageFileOffset = (Section->PageFileBacking.Offset >> PageShift) +
PageOffset;

MmpFreeFromPageFile(Section->PageFileBacking.DeviceHandle,
PageFileOffset,
PageCount);

PageFileOffset = (ImageBacking->Offset >> PageShift) + PageOffset;
MmpFreeFromPageFile(ImageBacking->DeviceHandle, PageFileOffset, PageCount);
return;
}

Expand Down Expand Up @@ -1262,7 +1252,12 @@ Return Value:
PageFile = INVALID_HANDLE;
if ((Section->Flags & IMAGE_SECTION_WAS_WRITABLE) != 0) {
if (Section->PageFileBacking.DeviceHandle == INVALID_HANDLE) {
Status = MmpAllocatePageFileSpace(Section);

ASSERT(IS_POINTER_ALIGNED(Section->VirtualAddress, PageSize));

Status = MmAllocatePageFileSpace(&(Section->PageFileBacking),
Section->Size);

if (!KSUCCESS(Status)) {
goto PageOutEnd;
}
Expand Down

0 comments on commit 7e9fa2b

Please sign in to comment.