Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help!!! Memory allocation!!!

28 views
Skip to first unread message

Danial.F

unread,
Jun 13, 2008, 2:51:00 AM6/13/08
to
Q1. In MSDN, it says that MmAllocateContiguousMemory first attemps to
allocate a continuous range of memory from Nonpaged pool, if it fails, then
allocates from unused pages. So, here, if it fails allcating from Nonpaged
pool and then successfully allocates the continuous range of memory, is this
continuous range of memory still nonpaged or not?

Q2. MmBuildMdlForNonPagedPool, this routine receives an MDL that specifies a
virtual memory buffer in nonpaged pool, and updates it to describe the
underlying physical pages. And the MDL virtual address that is input must be
within the nonpaged portion of system space. So, here, if
MmAllocateContiguousMemory fails to allocate the continuous memory in
nonpaged pool but successfully allocates from unused pages, can I still call
MmBuildMdlForNonPagedPool to build my MDL?

Q3. If a memory is allcated from nonpaged pool, do I still call
MmProbeAndLockPages and then call MmMapLockedPages to map a system space VA
to user space VA? or directly call MmMapLockedPages to map?

Q4.
In MSND, it says
VOID
MmProbeAndLockPages(
IN OUT PMDL MemoryDescriptorList,
IN KPROCESSOR_MODE AccessMode,
IN LOCK_OPERATION Operation
);
Operation
Specifies the type of operation for which the caller wants the access rights
probed and the pages locked, one of IoReadAccess, IoWriteAccess, or
IoModifyAccess.

If I want the access rights to be read, write and modify, how should I do?
Set the third parameter as IoReadAccess | IoWriteAccess | IoModifyAccess or
otherwise?

Pavel Lebedinsky [MSFT]

unread,
Jun 17, 2008, 6:08:29 AM6/17/08
to
> Q1. In MSDN, it says that MmAllocateContiguousMemory first attemps to
> allocate a continuous range of memory from Nonpaged pool, if it fails,
> then
> allocates from unused pages. So, here, if it fails allcating from Nonpaged
> pool and then successfully allocates the continuous range of memory, is
> this
> continuous range of memory still nonpaged or not?

It is.

> Q2. MmBuildMdlForNonPagedPool, this routine receives an MDL that specifies
> a
> virtual memory buffer in nonpaged pool, and updates it to describe the
> underlying physical pages. And the MDL virtual address that is input must
> be
> within the nonpaged portion of system space. So, here, if
> MmAllocateContiguousMemory fails to allocate the continuous memory in
> nonpaged pool but successfully allocates from unused pages, can I still
> call
> MmBuildMdlForNonPagedPool to build my MDL?

Yes.

> Q3. If a memory is allcated from nonpaged pool, do I still call
> MmProbeAndLockPages

Non-paged memory is already locked, there is no need to explicitly
lock it. MmProbeAndLockPages will actually assert on checked builds
if you give it an MDL with MDL_SOURCE_IS_NONPAGED_POOL
flag set.

> If I want the access rights to be read, write and modify, how should I do?
> Set the third parameter as IoReadAccess | IoWriteAccess | IoModifyAccess
> or
> otherwise?

Specify IoWriteAccess.


Dan Mihai [MSFT]

unread,
Jun 18, 2008, 10:28:39 AM6/18/08
to
Also, unless you are trying to map these physical pages into a UserMode
virtual address, don't call MmMapLockedPages either for an MDL created with
MmBuildMdlForNonPagedPool. MmMapLockedPages (AccessMode == KernelMode) would
waste another KernelMode virtual address mapping, because you already have a
KernelMode virtual address from the MmAllocateContiguousMemory call. Use
the VA returned by MmAllocateContiguousMemory.

I recommend testing this driver after "verifier.exe /standard /driver
mydriver.sys" on Windows Vista. Verifier will check all of these rules for
these API calls and will complain in case they are incorrect/wasteful.
http://msdn.microsoft.com/en-us/library/ms792871.aspx has information about
this.

Dan

--
This posting is provided "AS IS" with no warranties, and confers no rights.


"Pavel Lebedinsky [MSFT]" <pa...@online.microsoft.com> wrote in message
news:uaYoSIG0...@TK2MSFTNGP05.phx.gbl...

Danial.F

unread,
Jun 18, 2008, 12:19:00 PM6/18/08
to
So, if I do the following things,
pSystem = MmAllocateContiguousMemory (...); //1. the memory must be Nonpaged?
pMdl = IoAllocateMdl(pSystem...);
MmBuildMdlForNonPagedPool(pMdl); or MmProbeAndLockPages(pMdl)???
pUser = MmMapLockedPages(pMdl, UserMode...);// Map to user space address

are they correct? (Some params are omitted.)

Danial.F

unread,
Jun 18, 2008, 12:25:03 PM6/18/08
to
Acturally, I'm trying to map the continuous memory to user space address. I
dont know if the MmAllocateContiguousMemory didnt allocate the continuous
memory from non-paged pool but from paged pool or some other places, what
should I do? Lock it first whatever it is allocated from nonpaged pool or not
and then map it to user mode? or call MmBuildMdlForNonPagedPool even it is
not allocated from the nonpaged pool?

Pavel Lebedinsky [MSFT]

unread,
Jun 22, 2008, 1:19:08 AM6/22/08
to
> So, if I do the following things,
> pSystem = MmAllocateContiguousMemory (...); //1. the memory must be
> Nonpaged?
> pMdl = IoAllocateMdl(pSystem...);
> MmBuildMdlForNonPagedPool(pMdl); or MmProbeAndLockPages(pMdl)???
> pUser = MmMapLockedPages(pMdl, UserMode...);// Map to user space address
>
> are they correct? (Some params are omitted.)


Yes this should work. You don't need MmProbeAndLockPages here,
MmBuildMdlForNonPagedPool should be enough.

Note that MmAllocateContiguousMemory call can be very expensive,
and may not work at all for larger sizes (more than a few pages) once
physical memory gets fragmented. Do you really need this memory
to be physically contiguous?

Danial.F

unread,
Jun 22, 2008, 4:03:01 AM6/22/08
to
Yes, I do really need this memory to be physically contiguous. I may allocate
a large contiguous physical memory, if it fails, I try to allocate a smaller
one. Finally, I can always get the contiguous physical memory, though it may
be very small.

Alexander Grigoriev

unread,
Jun 22, 2008, 9:13:16 AM6/22/08
to
What's the reason you need it to be contiguous?

"Danial.F" <Dan...@discussions.microsoft.com> wrote in message
news:9D9C6B13-EDAC-4863...@microsoft.com...

0 new messages