TechTalkz.com Logo Ask the Expert

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Microsoft > Microsoft Device Drivers

Notices

Help!!! Memory allocation!!!

Microsoft Device Drivers


Reply
 
Thread Tools Display Modes
Old 13-06-2008, 12:46 PM   #1
Danial.F
Guest
 
Posts: n/a
Help!!! Memory allocation!!!

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?

  Reply With Quote
Old 17-06-2008, 03:52 PM   #2
Pavel Lebedinsky [MSFT]
Guest
 
Posts: n/a
Re: Help!!! Memory allocation!!!

> 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.


  Reply With Quote
Old 18-06-2008, 09:01 PM   #3
Dan Mihai [MSFT]
Guest
 
Posts: n/a
Re: Help!!! Memory allocation!!!

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]" <pavel@online.microsoft.com> wrote in message
news:uaYoSIG0IHA.2068@TK2MSFTNGP05.phx.gbl...
>> 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.
>


  Reply With Quote
Old 18-06-2008, 09:59 PM   #4
Danial.F
Guest
 
Posts: n/a
Re: Help!!! Memory allocation!!!

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.)



"Pavel Lebedinsky [MSFT]" wrote:

> > 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.
>
>
>

  Reply With Quote
Old 18-06-2008, 10:57 PM   #5
Danial.F
Guest
 
Posts: n/a
Re: Help!!! Memory allocation!!!

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?

"Dan Mihai [MSFT]" wrote:

> 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]" <pavel@online.microsoft.com> wrote in message
> news:uaYoSIG0IHA.2068@TK2MSFTNGP05.phx.gbl...
> >> 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.
> >

>
>

  Reply With Quote
Old 26-06-2008, 07:37 PM   #6
Pavel Lebedinsky [MSFT]
Guest
 
Posts: n/a
Re: Help!!! Memory allocation!!!

> 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?

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


  Reply With Quote
Old 26-06-2008, 07:37 PM   #7
Danial.F
Guest
 
Posts: n/a
Re: Help!!! Memory allocation!!!

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.

"Pavel Lebedinsky [MSFT]" wrote:

> > 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?
>
> --
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
>
>
>

  Reply With Quote
Old 26-06-2008, 07:37 PM   #8
Alexander Grigoriev
Guest
 
Posts: n/a
Re: Help!!! Memory allocation!!!

What's the reason you need it to be contiguous?

"Danial.F" <DanialF@discussions.microsoft.com> wrote in message
news:9D9C6B13-EDAC-4863-946C-2A11D09E7899@microsoft.com...
> 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.
>
> "Pavel Lebedinsky [MSFT]" wrote:
>
>> > 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?
>>
>> --
>> This posting is provided "AS IS" with no warranties, and confers no
>> rights.
>>
>>
>>



  Reply With Quote
Reply

Thread Tools
Display Modes



< Home - Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT +5.5. The time now is 11:35 AM.


vBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2010, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional