RE: Strange MDL behavior

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Thanks for your reply. It was much appreciated, but I'm afraid it didn't help
me much. Since I am in an IRP_MJ_READ request I am expected to write to the
buffer. Furthermore, the MDL_MAPPED_TO_SYSTEM_VA flag is not set, i.e., the
memory is not mapped to virtual memory yet, so no other thread can write to
the memory simultaneously.

However, after I posted this question I think I have come across a
explanation to what is happening. The memory manager has mapped some MDL
pages to a dummy page (http://www.microsoft.com/whdc/driver/tips/mdl.mspx).

"Carsten Schmidt" wrote:

Hi,

Sometimes I experience strange behavior when writing to the irp->MdlAddress
buffer of a disk filter driver I am writing.

In my IRP_MJ_READ routine I need to return the contents of an internal
buffer in stead of passing the request on to the disk driver.

If I enter the code below in the IRP_MJ_READ dispatch routine the assertion
occasionally fails, i.e., the RtlCopyMemory command did not copy correctly.


if (irp->MdlAddress) {
void* mdlBuffer = MmGetSystemAddressForMdlSafe(irp->MdlAddress,
NormalPagePriority);
ULONG length = IoGetCurrentIrpStackLocation(irp)->Parameters.Read.Length;
RtlCopyMemory(mdlBuffer, buffer, length);
ASSERT(RtlEqualMemory(mdlBuffer, buffer, length )); // <- This assertion
occasionally fails
}


Typically irp->MdlAddress->MdlFlags is (MDL_IO_PAGE_READ | MDL_PAGES_LOCKED)
before the MmGetSystemAddressForMdlSafe() call and (MDL_IO_PAGE_READ |
MDL_PAGES_LOCKED | MDL_MAPPED_TO_SYSTEM_VA) afterwards.

What really puzzles me is that after one ore more attempts the test code
below succeeds. It looks like the MDL is not fully ready yet on the first
attempt and that a separate thread is building it simultaneously.


do {
differs = FALSE;
RtlFillMemory(mdlBuffer, length, 'X');
for (i = 0; i < length; ++i) {
if (buffer[i] != 'X') {
differs = TRUE;
KdPrint( ("Byte %d of %d differs.\n", i, length) );
}
}
} while (differs);


Can anybody please tell me what is going on.

Thanks

.



Relevant Pages

  • Re: Alignment of disk-I/O from userland.
    ... >>the entire request over. ... > unlikely that the user's buffer will be in contiguous memory even if it ... This again is a job for busdma. ...
    (freebsd-arch)
  • Re: DMA operation to 64-bits PC platform
    ... completing the request with IoCompleteRequest? ... your application buffer. ... FPGA to PC memory) at its own initiative. ... The driver recognises this request and pends it indefinitely, ...
    (comp.arch.fpga)
  • Re: DMA operation to 64-bits PC platform
    ... completing the request with IoCompleteRequest? ... your application buffer. ... FPGA to PC memory) at its own initiative. ... The driver recognises this request and pends it indefinitely, ...
    (comp.arch.fpga)
  • Re: [Lit.] Buffer overruns
    ... of physical memory. ... >Overrun in that case was having the bits in a buffer spill over ... >I, an app program, request 100K core from the monitor. ... >>associate the cryptic error message with what they've done wrong. ...
    (sci.crypt)
  • Re: DMA operation to 64-bits PC platform
    ... completing the request with IoCompleteRequest? ... your application buffer. ... This is best done by specifying DO_DIRECT_IO as the DMA ... FPGA to PC memory) at its own initiative. ...
    (comp.arch.fpga)