Memory allocation failure

Hi,

This memory allocation function fails all the time. Can some one please help why its actually failing.

PHYSICAL_ADDRESS lowAddr;
PHYSICAL_ADDRESS highAddr;
PHYSICAL_ADDRESS skipBytes;
size_t allocated;
PMDL mdl;
// Set max/min physical memory addresses:
lowAddr.QuadPart = 0;
highAddr.QuadPart = (ULONGLONG)-1;
skipBytes.QuadPart = 0x200000;

// We’re going to have to create an MDL for each frame!!!
// This is zero filled, non-paged main memory pages:

mdl = MmAllocatePagesForMdlEx(lowAddr, highAddr, skipBytes , 0x200000,0,MM_ALLOCATE_PREFER_CONTIGUOUS|MM_ALLOCATE_REQUIRE_CONTIGUOUS_CHUNKS);

This always returns a NULL.

xxxxx@gmail.com wrote:

This memory allocation function fails all the time. Can some one please help why its actually failing.


// Set max/min physical memory addresses:
lowAddr.QuadPart = 0;
highAddr.QuadPart = (ULONGLONG)-1;
skipBytes.QuadPart = 0x200000;

// We’re going to have to create an MDL for each frame!!!
// This is zero filled, non-paged main memory pages:

mdl = MmAllocatePagesForMdlEx(lowAddr, highAddr, skipBytes , 0x200000,0,MM_ALLOCATE_PREFER_CONTIGUOUS|MM_ALLOCATE_REQUIRE_CONTIGUOUS_CHUNKS);

This always returns a NULL.

Which operating system? The flags you specified are Windows 7 and beyond.

I’m not sure you really want skipBytes=0x200000. If it can’t get 2MB
contiguous, that’s going to severely limit the chunks it can allocate.
I would change that to PAGE_SIZE.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

I am using Windows 7 64 bit version. Yes setting it to PAGE_SIZE works. Why does it fail at 2MB ?

Most likely because 2MB of contiguous memory is not available. If you
need contiguous memory make an effort to allocate it early, on one
project I had to make a legacy boot start driver just to allocate the
contiguous memory early enough to ensure it was acquired

Don Burn (MVP, Windows DKD)
Windows Filesystem and Driver Consulting
Website: http://www.windrvr.com
Blog: http://msmvps.com/blogs/WinDrvr

xxxxx@gmail.com” wrote in message
news:xxxxx@ntdev:

> I am using Windows 7 64 bit version. Yes setting it to PAGE_SIZE works. Why does it fail at 2MB ?

@Don There is large amount of free memory available. I have 16 gigs of free memory available. I dont know why its failing to allocate atleast 4 mb.

Memory can quickly get fragmented, you are asking for contiguous
physical memory.

Don Burn (MVP, Windows DKD)
Windows Filesystem and Driver Consulting
Website: http://www.windrvr.com
Blog: http://msmvps.com/blogs/WinDrvr

xxxxx@gmail.com” wrote in message
news:xxxxx@ntdev:

> @Don There is large amount of free memory available. I have 16 gigs of free memory available. I dont know why its failing to allocate atleast 4 mb.

> Most likely because 2MB of contiguous memory is not available.

IIRC, Pavel (from MSFT) said few weeks ago that 64-bit Windows versions use only large pages for the kernel. If this is the case, then the OP is requesting just a single kernel page. Therefore, I don’t think fragmentation should be the issue here, although it would more that likely be the one if we were speaking about 32-bit Windows…

Anton Bassov

xxxxx@gmail.com wrote:

I am using Windows 7 64 bit version. Yes setting it to PAGE_SIZE works. Why does it fail at 2MB ?

Did you read the description of what that does? If it can’t satisfy
your entire allocation in one contiguous chunk, that parameter says
there must be at least 2MB of space between consecutive chunks. It’s
hard for me to think of a scenario where that would be useful, but you
can see how that would make it hard to allocate the space.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

Large pages are (usually) automatically used to map the kernel image (ntoskrnl.exe), the HAL and the initial nonpaged pool. Most of the other memory in the system is still mapped using regular 4K pages.

The “require contiguous chunks” flag in MmAllocatePagesForMdlEx allows drivers to allocate pages in physically contiguous, aligned chunks. So if you set SkipPages to the size of the large page (2 MB on x64) you can allocate memory that could potentially be mapped with large pages. However, as of win7 there is no API to actually create a large page mapping, so currently this flag is of limited use.

That said, I’m surprised that with 16 GB of available pages the system is not able to allocate even a single contiguous large-page-aligned chunk. Normally you have to try pretty hard to achieve that kind of fragmentation. I wonder how much memory you can allocate this way if you do it immediately after system boots up?

Thanks,
Pavel

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@hotmail.com
Sent: Friday, November 19, 2010 1:16 PM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] Memory allocation failure

Most likely because 2MB of contiguous memory is not available.

IIRC, Pavel (from MSFT) said few weeks ago that 64-bit Windows versions use only large pages for the kernel. If this is the case, then the OP is requesting just a single kernel page. Therefore, I don’t think fragmentation should be the issue here, although it would more that likely be the one if we were speaking about 32-bit Windows…

Anton Bassov


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

If you do it right after the system boots, I have in the past
successfully gotten 1GB contiguous (I did not try larger). But I did
find that fragmentation occurred relatively quickly and that even 1MB
was questionable after a while.

Don Burn (MVP, Windows DKD)
Windows Filesystem and Driver Consulting
Website: http://www.windrvr.com
Blog: http://msmvps.com/blogs/WinDrvr

“Pavel Lebedynskiy” wrote in message
news:xxxxx@ntdev:

> Large pages are (usually) automatically used to map the kernel image (ntoskrnl.exe), the HAL and the initial nonpaged pool. Most of the other memory in the system is still mapped using regular 4K pages.
>
> The “require contiguous chunks” flag in MmAllocatePagesForMdlEx allows drivers to allocate pages in physically contiguous, aligned chunks. So if you set SkipPages to the size of the large page (2 MB on x64) you can allocate memory that could potentially be mapped with large pages. However, as of win7 there is no API to actually create a large page mapping, so currently this flag is of limited use.
>
> That said, I’m surprised that with 16 GB of available pages the system is not able to allocate even a single contiguous large-page-aligned chunk. Normally you have to try pretty hard to achieve that kind of fragmentation. I wonder how much memory you can allocate this way if you do it immediately after system boots up?
>
> Thanks,
> Pavel
>
> -----Original Message-----
> From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@hotmail.com
> Sent: Friday, November 19, 2010 1:16 PM
> To: Windows System Software Devs Interest List
> Subject: RE:[ntdev] Memory allocation failure
>
>
> > Most likely because 2MB of contiguous memory is not available.
>
> IIRC, Pavel (from MSFT) said few weeks ago that 64-bit Windows versions use only large pages for the kernel. If this is the case, then the OP is requesting just a single kernel page. Therefore, I don’t think fragmentation should be the issue here, although it would more that likely be the one if we were speaking about 32-bit Windows…
>
>
> Anton Bassov
>
> —
> NTDEV is sponsored by OSR
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

This is probably because the address space has become fragmented. A trick
Ed Dekker did some years ago was to write two drivers, one of which loaded
early and reserved the space, and the other of which he could later load and
unload dynamically (while testing) and which asked the first driver for a
pointer to the block of memory it allocated. The trick was to force the
first driver to load early (it had to be a boot-time driver, for example).
The hazard of memory address fragmentation is that you have virtually (pun
intended) no control over when and how it happens once you start letting
other drivers load. I’m not sure how this would be pulled off in a WDF
context but others should be able to answer that.

The problem he faced was that the DMA card didn’t have any scatter/gather
capability and required a massive contiguous buffer. This is the inevitable
consequence of letting hardware engineers make design decisions; for
example, this decision probably saved $2/card on a product that might have
had 1000 cards produced, so the engineer saved $2,000 in development costs.
This increased the cost of writing the driver by substantially more than
$2,000. Hardware engineers need to understand that their decisions have
serious implications in the context of real operating systems (for example,
we’ve both seen devices for which it would not be possible to write a device
driver in any real operating system, such as Windows, Unix, linux, Solaris,
Mac OS X, etc., but the engineer who designed the card failed to see that
there was any problem because he could write a driver on his bare MS-DOS
machine or under the embedded RTOS on his desktop development system, by
hijacking interrupts, reprogramming the counter-timer chip, etc. The number
of failures I’ve seen like this in the last 20 years is astounding, because
hardware engineers come equipped with “hardware engineer blinders”
pre-installed, rendering them incapable of seeing firmware, software, etc.
as issues that need to be addressed in the design)
joe

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of
xxxxx@gmail.com
Sent: Friday, November 19, 2010 2:10 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] Memory allocation failure

Hi,

This memory allocation function fails all the time. Can some one
please help why its actually failing.

PHYSICAL_ADDRESS lowAddr;
PHYSICAL_ADDRESS highAddr;
PHYSICAL_ADDRESS skipBytes;
size_t allocated;
PMDL mdl;
// Set max/min physical memory addresses:
lowAddr.QuadPart = 0;
highAddr.QuadPart = (ULONGLONG)-1;
skipBytes.QuadPart = 0x200000;

// We’re going to have to create an MDL for each frame!!!
// This is zero filled, non-paged main memory pages:

mdl = MmAllocatePagesForMdlEx(lowAddr, highAddr, skipBytes ,
0x200000,0,MM_ALLOCATE_PREFER_CONTIGUOUS|MM_ALLOCATE_REQUIRE_CONTIGUOUS_CHUN
KS);

This always returns a NULL.


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


This message has been scanned for viruses and dangerous content by
MailScanner, and is believed to be clean.

Maybe it’s not such a problem these days with everything supporting SG,
but are there any whitepapers on minimizing memory fragmentation in your
driver? eg so your driver isn’t the cause of the problem

I can think of a bunch of obvious things that might cause fragmentation
but maybe there is some techniques I’ve never thought of.

Do lookaside lists help at all?

James

This is probably because the address space has become fragmented. A
trick
Ed Dekker did some years ago was to write two drivers, one of which
loaded
early and reserved the space, and the other of which he could later
load and
unload dynamically (while testing) and which asked the first driver
for a
pointer to the block of memory it allocated. The trick was to force
the
first driver to load early (it had to be a boot-time driver, for
example).
The hazard of memory address fragmentation is that you have virtually
(pun
intended) no control over when and how it happens once you start
letting
other drivers load. I’m not sure how this would be pulled off in a WDF
context but others should be able to answer that.

The problem he faced was that the DMA card didn’t have any
scatter/gather
capability and required a massive contiguous buffer. This is the
inevitable
consequence of letting hardware engineers make design decisions; for
example, this decision probably saved $2/card on a product that might
have
had 1000 cards produced, so the engineer saved $2,000 in development
costs.
This increased the cost of writing the driver by substantially more
than
$2,000. Hardware engineers need to understand that their decisions
have
serious implications in the context of real operating systems (for
example,
we’ve both seen devices for which it would not be possible to write a
device
driver in any real operating system, such as Windows, Unix, linux,
Solaris,
Mac OS X, etc., but the engineer who designed the card failed to see
that
there was any problem because he could write a driver on his bare
MS-DOS
machine or under the embedded RTOS on his desktop development system,
by
hijacking interrupts, reprogramming the counter-timer chip, etc. The
number
of failures I’ve seen like this in the last 20 years is astounding,
because
hardware engineers come equipped with “hardware engineer blinders”
pre-installed, rendering them incapable of seeing firmware, software,
etc.
as issues that need to be addressed in the design)
joe

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of
xxxxx@gmail.com
Sent: Friday, November 19, 2010 2:10 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] Memory allocation failure

Hi,

This memory allocation function fails all the time. Can some one
please help why its actually failing.

PHYSICAL_ADDRESS lowAddr;
PHYSICAL_ADDRESS highAddr;
PHYSICAL_ADDRESS skipBytes;
size_t allocated;
PMDL mdl;
// Set max/min physical memory addresses:
lowAddr.QuadPart = 0;
highAddr.QuadPart = (ULONGLONG)-1;
skipBytes.QuadPart = 0x200000;

// We’re going to have to create an MDL for each frame!!!
// This is zero filled, non-paged main memory pages:

mdl = MmAllocatePagesForMdlEx(lowAddr, highAddr, skipBytes ,

0x200000,0,MM_ALLOCATE_PREFER_CONTIGUOUS|MM_ALLOCATE_REQUIRE_CONTIGUOUS_
CHUN

KS);

This always returns a NULL.


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


This message has been scanned for viruses and dangerous content by
MailScanner, and is believed to be clean.


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

I don’t think lookaside will help in this case, since it is subject to trimming and reallocating …

Memory Allocator is a complex trade off. What are the ranges of sizes for the blocks, and their corresponding numbers etc. influence the probability of grabing multiple physically contiguous pages or region.

And different OS have different implementations, along with different flavors of them side by side…

-pro

On Nov 19, 2010, at 10:07 PM, James Harper wrote:

Maybe it’s not such a problem these days with everything supporting SG,
but are there any whitepapers on minimizing memory fragmentation in your
driver? eg so your driver isn’t the cause of the problem

I can think of a bunch of obvious things that might cause fragmentation
but maybe there is some techniques I’ve never thought of.

Do lookaside lists help at all?

James

>
> This is probably because the address space has become fragmented. A
trick
> Ed Dekker did some years ago was to write two drivers, one of which
loaded
> early and reserved the space, and the other of which he could later
load and
> unload dynamically (while testing) and which asked the first driver
for a
> pointer to the block of memory it allocated. The trick was to force
the
> first driver to load early (it had to be a boot-time driver, for
example).
> The hazard of memory address fragmentation is that you have virtually
(pun
> intended) no control over when and how it happens once you start
letting
> other drivers load. I’m not sure how this would be pulled off in a WDF
> context but others should be able to answer that.
>
> The problem he faced was that the DMA card didn’t have any
scatter/gather
> capability and required a massive contiguous buffer. This is the
inevitable
> consequence of letting hardware engineers make design decisions; for
> example, this decision probably saved $2/card on a product that might
have
> had 1000 cards produced, so the engineer saved $2,000 in development
costs.
> This increased the cost of writing the driver by substantially more
than
> $2,000. Hardware engineers need to understand that their decisions
have
> serious implications in the context of real operating systems (for
example,
> we’ve both seen devices for which it would not be possible to write a
device
> driver in any real operating system, such as Windows, Unix, linux,
Solaris,
> Mac OS X, etc., but the engineer who designed the card failed to see
that
> there was any problem because he could write a driver on his bare
MS-DOS
> machine or under the embedded RTOS on his desktop development system,
by
> hijacking interrupts, reprogramming the counter-timer chip, etc. The
number
> of failures I’ve seen like this in the last 20 years is astounding,
because
> hardware engineers come equipped with “hardware engineer blinders”
> pre-installed, rendering them incapable of seeing firmware, software,
etc.
> as issues that need to be addressed in the design)
> joe
>
>
> -----Original Message-----
> From: xxxxx@lists.osr.com
> [mailto:xxxxx@lists.osr.com] On Behalf Of
> xxxxx@gmail.com
> Sent: Friday, November 19, 2010 2:10 PM
> To: Windows System Software Devs Interest List
> Subject: [ntdev] Memory allocation failure
>
> Hi,
>
> This memory allocation function fails all the time. Can some one
> please help why its actually failing.
>
> PHYSICAL_ADDRESS lowAddr;
> PHYSICAL_ADDRESS highAddr;
> PHYSICAL_ADDRESS skipBytes;
> size_t allocated;
> PMDL mdl;
> // Set max/min physical memory addresses:
> lowAddr.QuadPart = 0;
> highAddr.QuadPart = (ULONGLONG)-1;
> skipBytes.QuadPart = 0x200000;
>
>
> // We’re going to have to create an MDL for each frame!!!
> // This is zero filled, non-paged main memory pages:
>
> mdl = MmAllocatePagesForMdlEx(lowAddr, highAddr, skipBytes ,
>
0x200000,0,MM_ALLOCATE_PREFER_CONTIGUOUS|MM_ALLOCATE_REQUIRE_CONTIGUOUS_
CHUN
> KS);
>
> This always returns a NULL.
>
> —
> NTDEV is sponsored by OSR
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer
>
> –
> This message has been scanned for viruses and dangerous content by
> MailScanner, and is believed to be clean.
>
>
> —
> NTDEV is sponsored by OSR
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

Joe,

You bring up some interesting points, and I guess it comes from vast amount of experience. With due respect, this should go to nttalk. I think you can subscribe to that using osronline site !

It is not that HW engineering is so much of a problem. It is the specification and abstraction they often forget to provide. And of course there are latent bugs in chipset or in the firmware/bootcodes. Though I see almost all chip vendors who are specialized in the FC, NIC, and switch products have some published source code in the Linux kernel tree, and they often keep them updated - the clarity of the codes and their underlying mechanics are not explained… But then again, some of the devices are so complex that writing a good specification would take 3 to 4 of the 2.5 inches folder - printing 2 soft pages into one side of a printed page.
In sheer number of pages, 5000 plus.

This becomes very hard when it comes to adapt those chipset in to new technologies that are not yet specification wise complete by the standard body like PCI sig, IEEE working groups…

For commodity type peripheral, usually the vendors provide most drivers!

-pro

On Nov 19, 2010, at 9:58 PM, Joseph M. Newcomer wrote:

This is probably because the address space has become fragmented. A trick
Ed Dekker did some years ago was to write two drivers, one of which loaded
early and reserved the space, and the other of which he could later load and
unload dynamically (while testing) and which asked the first driver for a
pointer to the block of memory it allocated. The trick was to force the
first driver to load early (it had to be a boot-time driver, for example).
The hazard of memory address fragmentation is that you have virtually (pun
intended) no control over when and how it happens once you start letting
other drivers load. I’m not sure how this would be pulled off in a WDF
context but others should be able to answer that.

The problem he faced was that the DMA card didn’t have any scatter/gather
capability and required a massive contiguous buffer. This is the inevitable
consequence of letting hardware engineers make design decisions; for
example, this decision probably saved $2/card on a product that might have
had 1000 cards produced, so the engineer saved $2,000 in development costs.
This increased the cost of writing the driver by substantially more than
$2,000. Hardware engineers need to understand that their decisions have
serious implications in the context of real operating systems (for example,
we’ve both seen devices for which it would not be possible to write a device
driver in any real operating system, such as Windows, Unix, linux, Solaris,
Mac OS X, etc., but the engineer who designed the card failed to see that
there was any problem because he could write a driver on his bare MS-DOS
machine or under the embedded RTOS on his desktop development system, by
hijacking interrupts, reprogramming the counter-timer chip, etc. The number
of failures I’ve seen like this in the last 20 years is astounding, because
hardware engineers come equipped with “hardware engineer blinders”
pre-installed, rendering them incapable of seeing firmware, software, etc.
as issues that need to be addressed in the design)
joe

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of
xxxxx@gmail.com
Sent: Friday, November 19, 2010 2:10 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] Memory allocation failure

Hi,

This memory allocation function fails all the time. Can some one
please help why its actually failing.

PHYSICAL_ADDRESS lowAddr;
PHYSICAL_ADDRESS highAddr;
PHYSICAL_ADDRESS skipBytes;
size_t allocated;
PMDL mdl;
// Set max/min physical memory addresses:
lowAddr.QuadPart = 0;
highAddr.QuadPart = (ULONGLONG)-1;
skipBytes.QuadPart = 0x200000;

// We’re going to have to create an MDL for each frame!!!
// This is zero filled, non-paged main memory pages:

mdl = MmAllocatePagesForMdlEx(lowAddr, highAddr, skipBytes ,
0x200000,0,MM_ALLOCATE_PREFER_CONTIGUOUS|MM_ALLOCATE_REQUIRE_CONTIGUOUS_CHUN
KS);

This always returns a NULL.


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


This message has been scanned for viruses and dangerous content by
MailScanner, and is believed to be clean.


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

I cannot subscribe to nttalk because I do not allow any form of scripting,
such as JavaVirus, to pass three levels of firewall. A friend subscribed me
to these newsgroups, and I don’t even know how to log into the OSR site.
Once you’ve been taken out for a week (and a year of cleanup) by a nasty
scripting virus, you get more than a little twitchy about allowing
client-side scripting (which I consider a hostile attack)

I can accept bugs in hardware. What I can’t accept are designs that are
completely unrealistic (recent discussion in one forum: “I have this device
which connects to the printer port, and I have to poke it every 100
microseconds. How can I do this from an application? I can’t seem to find
a timer with resolution of less than 1ms!” An obvious example of a device
specification that was totally unrealistic, and an notion that an
application can do something repeatedly every 100us without fail goes beyond
unrealistic into high fantasy). The sort of drivers you are describing are
clearly buildable, even if we don’t have good specs.

A custom processor chip I consulted on some years ago did not allow the
stack pointer to be read; if you tried to read the stack pointer register,
you got some unrelated value, some CPU status register. I asked “How do I
read the stack pointer?” and was assured by the engineer, “Oh, the software
tracks it for you!” Since I was supposed to write “the” software [in fact,
a debugger], I asked how it was supposed to do that. “No problem, software
does that sort of thing all the time!” Ultimately, I had to turn the job
down because there was not enough support in the hardware to make it
feasible to write a debugger. The myth that “the” software (application?
OS? Debugger?) magically exists is the major weakness of hardware engineers,
who somehow think we have magical powers to accomplish things that should be
supported by the hardware. Note: you never heard of this chip, and to me it
is obvious why. Nobody could actually write support software for the
developers. It disappeared without a trace in the mid-1990s.

As one of the “vendors” who is often requested to build a driver for a
device that cannot possibly work, I’ve had to tell several potential clients
that the design was unprogrammable, and anyone who claimed otherwise was
probably stealing their money, and I wouldn’t touch the project. Others who
are more active in writing drivers have their own horror stories to tell.

The issue is not the drivers we see (although the old non-WDF smart card
driver was so full of design errors as to be a great example of how to make
every mistake in the book), but in the hardware we don’t see because nobody
could build a driver for it under any conditions imaginable (the 100us from
application space example should never have gotten beyond the paper-design
stage if anyone competent in software had been called in before it was
built).

I once spent three days reverse-engineering one peripheral because there was
no way to set it to its initial state, or query what state it was in; I had
to guess what state it was in by poking values at registers and reading
other values out of other registers, and developing a state machine model of
what states produced what results. When I asked the designer why there was
no way to discover the state or reset it, he assured me it was not necessary
because “the” software would always track it. When I pointed out the
obvious, that I was writing “the” software and always needed to know what
state it was in because the user could do weird things in the application
and then unload the driver, I was assured that their one software expert
said that a state register was a waste of gates because he knew how to track
the state. I asked if I could talk to him, so he could explain to me how he
used psychic vibrations to infer the state. Note that a reboot could not
reset the card; so while debugging the driver, I had to shut off the machine
and completely reboot on each test. Ultimately, the power cycling destroyed
my (in those days monochrome) display, so I had to add three days of
development time so I would not lose the replacement display as well. Since
the user could hard-reboot at any time (this was not Windows!) and leave the
card in an indeterminate state (it did not reset on a bus-reset signal!), I
still had to work correctly in spite of this. So the scenario was not
restricted to just doing driver development.

Hardware design is too important to leave to hardware designers!
joe

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Prokash Sinha
Sent: Saturday, November 20, 2010 11:58 AM
To: Windows System Software Devs Interest List
Subject: Re: [ntdev] Memory allocation failure

Joe,

You bring up some interesting points, and I guess it comes from vast amount
of experience. With due respect, this should go to nttalk. I think you can
subscribe to that using osronline site !

It is not that HW engineering is so much of a problem. It is the
specification and abstraction they often forget to provide. And of course
there are latent bugs in chipset or in the firmware/bootcodes. Though I see
almost all chip vendors who are specialized in the FC, NIC, and switch
products have some published source code in the Linux kernel tree, and they
often keep them updated - the clarity of the codes and their underlying
mechanics are not explained… But then again, some of the devices are so
complex that writing a good specification would take 3 to 4 of the 2.5
inches folder - printing 2 soft pages into one side of a printed page.
In sheer number of pages, 5000 plus.

This becomes very hard when it comes to adapt those chipset in to new
technologies that are not yet specification wise complete by the standard
body like PCI sig, IEEE working groups…

For commodity type peripheral, usually the vendors provide most drivers!

-pro

On Nov 19, 2010, at 9:58 PM, Joseph M. Newcomer wrote:

This is probably because the address space has become fragmented. A
trick Ed Dekker did some years ago was to write two drivers, one of
which loaded early and reserved the space, and the other of which he
could later load and unload dynamically (while testing) and which
asked the first driver for a pointer to the block of memory it
allocated. The trick was to force the first driver to load early (it had
to be a boot-time driver, for example).
The hazard of memory address fragmentation is that you have virtually
(pun
intended) no control over when and how it happens once you start
letting other drivers load. I’m not sure how this would be pulled off
in a WDF context but others should be able to answer that.

The problem he faced was that the DMA card didn’t have any
scatter/gather capability and required a massive contiguous buffer.
This is the inevitable consequence of letting hardware engineers make
design decisions; for example, this decision probably saved $2/card on
a product that might have had 1000 cards produced, so the engineer saved
$2,000 in development costs.
This increased the cost of writing the driver by substantially more
than $2,000. Hardware engineers need to understand that their
decisions have serious implications in the context of real operating
systems (for example, we’ve both seen devices for which it would not
be possible to write a device driver in any real operating system,
such as Windows, Unix, linux, Solaris, Mac OS X, etc., but the
engineer who designed the card failed to see that there was any
problem because he could write a driver on his bare MS-DOS machine or
under the embedded RTOS on his desktop development system, by
hijacking interrupts, reprogramming the counter-timer chip, etc. The
number of failures I’ve seen like this in the last 20 years is astounding,
because hardware engineers come equipped with “hardware engineer blinders”
pre-installed, rendering them incapable of seeing firmware, software, etc.
as issues that need to be addressed in the design)
joe

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of
xxxxx@gmail.com
Sent: Friday, November 19, 2010 2:10 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] Memory allocation failure

Hi,

This memory allocation function fails all the time. Can some one
please help why its actually failing.

PHYSICAL_ADDRESS lowAddr;
PHYSICAL_ADDRESS highAddr;
PHYSICAL_ADDRESS skipBytes;
size_t allocated;
PMDL mdl;
// Set max/min physical memory addresses:
lowAddr.QuadPart = 0;
highAddr.QuadPart = (ULONGLONG)-1;
skipBytes.QuadPart = 0x200000;

// We’re going to have to create an MDL for each frame!!!
// This is zero filled, non-paged main memory pages:

mdl = MmAllocatePagesForMdlEx(lowAddr, highAddr, skipBytes ,
0x200000,0,MM_ALLOCATE_PREFER_CONTIGUOUS|MM_ALLOCATE_REQUIRE_CONTIGUOU
S_CHUN
KS);

This always returns a NULL.


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


This message has been scanned for viruses and dangerous content by
MailScanner, and is believed to be clean.


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


This message has been scanned for viruses and dangerous content by
MailScanner, and is believed to be clean.

You have to distinguish between block-level fragmentation (which can be
reduced by lookaside lists) and page-level fragmentation (which is what
limits the size block you are likely to get when you request huge blocks).
We usually refer to the latter problem as address space fragmentation. They
are abstractly the same problem, but the approaches to them differ
considerably. The biggest problem is that while you can have some control
over the block-level fragmentation within your driver, you have zero control
over page-level fragmentation, because this is an artifact of the
interactions of kernel components you never heard of (including other
drivers) with the kernel’s allocation mechanisms for managing kernel space.

What is SG? [google gave me a lot of information about buying glass in
Singapore, but nothing else seemed relevant to the acronym]
joe

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of James Harper
Sent: Saturday, November 20, 2010 1:07 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] Memory allocation failure

Maybe it’s not such a problem these days with everything supporting SG, but
are there any whitepapers on minimizing memory fragmentation in your driver?
eg so your driver isn’t the cause of the problem

I can think of a bunch of obvious things that might cause fragmentation but
maybe there is some techniques I’ve never thought of.

Do lookaside lists help at all?

James

This is probably because the address space has become fragmented. A
trick
Ed Dekker did some years ago was to write two drivers, one of which
loaded
early and reserved the space, and the other of which he could later
load and
unload dynamically (while testing) and which asked the first driver
for a
pointer to the block of memory it allocated. The trick was to force
the
first driver to load early (it had to be a boot-time driver, for
example).
The hazard of memory address fragmentation is that you have virtually
(pun
intended) no control over when and how it happens once you start
letting
other drivers load. I’m not sure how this would be pulled off in a WDF
context but others should be able to answer that.

The problem he faced was that the DMA card didn’t have any
scatter/gather
capability and required a massive contiguous buffer. This is the
inevitable
consequence of letting hardware engineers make design decisions; for
example, this decision probably saved $2/card on a product that might
have
had 1000 cards produced, so the engineer saved $2,000 in development
costs.
This increased the cost of writing the driver by substantially more
than
$2,000. Hardware engineers need to understand that their decisions
have
serious implications in the context of real operating systems (for
example,
we’ve both seen devices for which it would not be possible to write a
device
driver in any real operating system, such as Windows, Unix, linux,
Solaris,
Mac OS X, etc., but the engineer who designed the card failed to see
that
there was any problem because he could write a driver on his bare
MS-DOS
machine or under the embedded RTOS on his desktop development system,
by
hijacking interrupts, reprogramming the counter-timer chip, etc. The
number
of failures I’ve seen like this in the last 20 years is astounding,
because
hardware engineers come equipped with “hardware engineer blinders”
pre-installed, rendering them incapable of seeing firmware, software,
etc.
as issues that need to be addressed in the design)
joe

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of
xxxxx@gmail.com
Sent: Friday, November 19, 2010 2:10 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] Memory allocation failure

Hi,

This memory allocation function fails all the time. Can some one
please help why its actually failing.

PHYSICAL_ADDRESS lowAddr;
PHYSICAL_ADDRESS highAddr;
PHYSICAL_ADDRESS skipBytes;
size_t allocated;
PMDL mdl;
// Set max/min physical memory addresses:
lowAddr.QuadPart = 0;
highAddr.QuadPart = (ULONGLONG)-1;
skipBytes.QuadPart = 0x200000;

// We’re going to have to create an MDL for each frame!!!
// This is zero filled, non-paged main memory pages:

mdl = MmAllocatePagesForMdlEx(lowAddr, highAddr, skipBytes ,

0x200000,0,MM_ALLOCATE_PREFER_CONTIGUOUS|MM_ALLOCATE_REQUIRE_CONTIGUOUS_
CHUN

KS);

This always returns a NULL.


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


This message has been scanned for viruses and dangerous content by
MailScanner, and is believed to be clean.


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


This message has been scanned for viruses and dangerous content by
MailScanner, and is believed to be clean.

>

What is SG? [google gave me a lot of information about buying glass in
Singapore, but nothing else seemed relevant to the acronym]

Scatter Gather, as in NdisMAllocateNetBufferSGList. I think it’s
abbreviated more regularly under the Linux kernel which is why I thought
I could get away with it here :slight_smile:

James

I’m assuming we are talking about physical fragmentation here (because that was the reason for the contiguous memory allocation failure the OP complained about), not virtual address space fragmentation (which is what the other fork of the thread seems to have drifted into).

Physical fragmentation is caused by pages that are locked in memory, such as nonpaged pool, probe-and-locked user memory, pages allocated by MmAllocatePagesForMdlEx etc. So here are a few things you can do in order to reduce this type of fragmentation:

  1. Allocate less.
  2. Allocate pageable memory when possible.
  3. Avoid locking large user buffers for a long time.
  4. When calling MmAllocatePagesForMdlEx, specify the MM_ALLOCATE_PREFER_CONTIGUOUS flag (unless this is a performance critical path, or the allocation is short-lived).

Up to a certain point, allocating nonpaged pool does not increase physical fragmentation. This is because the memory manager pre-allocates 3% of the total RAM for the so called “initial nonpaged pool”, which is physically contiguous (and typically mapped with large pages). However, if/when the initial pool is exhausted (or when its unused portions are discarded after the system runs out of free/zeroed pages), further attempts to expand the pool will be satisfied using random pages, and that can fragment physical memory very quickly. So if you want to maximize the amount of available contiguous memory, in some cases it might make sense to convert large long-lived nonpaged pool allocations to MmAllocatePagesForMdlEx with the “prefer contiguous” flag (I wouldn’t recommend this as a general practice though).

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of James Harper
Sent: Friday, November 19, 2010 10:07 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] Memory allocation failure

Maybe it’s not such a problem these days with everything supporting SG, but are there any whitepapers on minimizing memory fragmentation in your driver? eg so your driver isn’t the cause of the problem

I can think of a bunch of obvious things that might cause fragmentation but maybe there is some techniques I’ve never thought of.

Do lookaside lists help at all?

James

This is probably because the address space has become fragmented. A
trick
Ed Dekker did some years ago was to write two drivers, one of which
loaded
early and reserved the space, and the other of which he could later
load and
unload dynamically (while testing) and which asked the first driver
for a
pointer to the block of memory it allocated. The trick was to force
the
first driver to load early (it had to be a boot-time driver, for
example).
The hazard of memory address fragmentation is that you have virtually
(pun
intended) no control over when and how it happens once you start
letting
other drivers load. I’m not sure how this would be pulled off in a WDF
context but others should be able to answer that.

The problem he faced was that the DMA card didn’t have any
scatter/gather
capability and required a massive contiguous buffer. This is the
inevitable
consequence of letting hardware engineers make design decisions; for
example, this decision probably saved $2/card on a product that might
have
had 1000 cards produced, so the engineer saved $2,000 in development
costs.
This increased the cost of writing the driver by substantially more
than
$2,000. Hardware engineers need to understand that their decisions
have
serious implications in the context of real operating systems (for
example,
we’ve both seen devices for which it would not be possible to write a
device
driver in any real operating system, such as Windows, Unix, linux,
Solaris,
Mac OS X, etc., but the engineer who designed the card failed to see
that
there was any problem because he could write a driver on his bare
MS-DOS
machine or under the embedded RTOS on his desktop development system,
by
hijacking interrupts, reprogramming the counter-timer chip, etc. The
number
of failures I’ve seen like this in the last 20 years is astounding,
because
hardware engineers come equipped with “hardware engineer blinders”
pre-installed, rendering them incapable of seeing firmware, software,
etc.
as issues that need to be addressed in the design)
joe

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of
xxxxx@gmail.com
Sent: Friday, November 19, 2010 2:10 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] Memory allocation failure

Hi,

This memory allocation function fails all the time. Can some one
please help why its actually failing.

PHYSICAL_ADDRESS lowAddr;
PHYSICAL_ADDRESS highAddr;
PHYSICAL_ADDRESS skipBytes;
size_t allocated;
PMDL mdl;
// Set max/min physical memory addresses:
lowAddr.QuadPart = 0;
highAddr.QuadPart = (ULONGLONG)-1;
skipBytes.QuadPart = 0x200000;

// We’re going to have to create an MDL for each frame!!!
// This is zero filled, non-paged main memory pages:

mdl = MmAllocatePagesForMdlEx(lowAddr, highAddr, skipBytes ,

0x200000,0,MM_ALLOCATE_PREFER_CONTIGUOUS|MM_ALLOCATE_REQUIRE_CONTIGUOUS_
CHUN

KS);

This always returns a NULL.


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


This message has been scanned for viruses and dangerous content by
MailScanner, and is believed to be clean.


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

Joseph M. Newcomer wrote:

I once spent three days reverse-engineering one peripheral because there was
no way to set it to its initial state, or query what state it was in; I had
to guess what state it was in by poking values at registers and reading
other values out of other registers, and developing a state machine model of
what states produced what results. When I asked the designer why there was
no way to discover the state or reset it, he assured me it was not necessary
because “the” software would always track it. When I pointed out the
obvious, that I was writing “the” software and always needed to know what
state it was in because the user could do weird things in the application
and then unload the driver, I was assured that their one software expert
said that a state register was a waste of gates because he knew how to track
the state.

That’s certainly familiar. One of my clients has a USB camera with
adjustable brightness, zoom, focus, and white balance. There is no
ability to read the current state of any of the controls, nor is there a
way to set an absolute value. You send a command saying “start
increasing the brightness”, then delay a bit, then send another command
saying “stop adjusting brightness.” There’s no way to know how far it
adjusted, nor when it has reached a stop.

Now, that design might have worked OK when the camera was embedded in a
device with a custom control panel with up and down buttons, but it is
essentially impossible to integrate that with Windows and DirectShow.
It’s too bad, because the image quality is pretty good.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.