"Extreme DMA" and Interlocked queues

As part of a new driver, I thought I’d have a crack at PV’s “Extreme DMA”
method, published in 2001. It looks, as he claims, fairly straightforward
but there is one question I can’t figure out:

Several times he specifies the use of the interlocked queuing functions from
within the ISR. But these functions require a spinlock and the DDK says
“Callers of KeAcquireSpinlock must be running at IRQL <= DISPATCH_LEVEL”.

So, what am I missing?

Don Ward

Don,

The Interlocked queues use the spinlock in a special way. This allows
them to be called at any level up to interrupt level for the device. This
is buried down in the documentation for these functions as:

“If the caller uses only ExInterlocked…List routines to manipulate the
list, these routines can be called from a single IRQL that is <= DIRQL. If
other driver routines access the list using any other routines, such as the
noninterlocked InsertHeadList, callers of ExInterlocked…List must be at <=
DISPATCH_LEVEL”


Don Burn (MVP, Windows DDK)
Windows 2k/XP/2k3 Filesystem and Driver Consulting

“Don Ward” wrote in message news:xxxxx@ntdev…
As part of a new driver, I thought I’d have a crack at PV’s “Extreme DMA”
method, published in 2001. It looks, as he claims, fairly straightforward
but there is one question I can’t figure out:

Several times he specifies the use of the interlocked queuing functions from
within the ISR. But these functions require a spinlock and the DDK says
“Callers of KeAcquireSpinlock must be running at IRQL <= DISPATCH_LEVEL”.

So, what am I missing?

Don Ward

Don Burn wrote:

Don,

The Interlocked queues use the spinlock in a special way. This allows
them to be called at any level up to interrupt level for the device.

Don’s exactly right. You just have to “know” this, as part of the lore.
And understand the cryptic comments in the docs.

Is there anybody left who wonders why people find it so tough to write
WDM drivers? Sigh. I can’t get to WDF fast enough…

The Tao of Chi says (loosely translated): “The way that can be written
is not the true way.” As for eternal truth, so for WDM drivers.

Peter
OSR

Thanks for the response; the mists begin to clear but I still don’t really
understand how it works.

There are two choices for the queue spinlock:
a) The Interrupt spinlock
b) some other spinlock

If I choose a), when the ISR uses ExInterlockedInsertHeadList, wouldn’t it
attempt to claim a spin lock it already has ?
I think option b) might deadlock on a Uniprocessor: Imagine the DPCforISR is
extracting stuff from the “done” queue when another interrupt occurs: the
ISR preempts it and spins waiting for the DPC to free the lock (which it
can’t because the ISR has preempted it by spinning).

Clearly that isn’t happening, otherwise everybody who has tried the “Extreme
DMA” would be complaining that it locks up sometimes. So something else,
something special, must be happening.

I haven’t actually tried any of this yet, I’m just trying to understand how
it all works before I start (an old-fashioned attitude, but it works for
me).

Regards

Don Ward

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Don Burn
Sent: 14 December 2004 20:28
To: Windows System Software Devs Interest List
Subject: Re:[ntdev] “Extreme DMA” and Interlocked queues

Don,

The Interlocked queues use the spinlock in a special
way. This allows them to be called at any level up to
interrupt level for the device. This is buried down in the
documentation for these functions as:

“If the caller uses only ExInterlocked…List routines to
manipulate the list, these routines can be called from a
single IRQL that is <= DIRQL. If other driver routines access
the list using any other routines, such as the noninterlocked
InsertHeadList, callers of ExInterlocked…List must be at <=
DISPATCH_LEVEL”


Don Burn (MVP, Windows DDK)
Windows 2k/XP/2k3 Filesystem and Driver Consulting

“Don Ward” wrote in message
news:xxxxx@ntdev… As part of a new driver, I thought I’d have a crack at
PV’s “Extreme DMA” method, published in 2001. It looks, as he claims, fairly
straightforward but there is one question I can’t figure out:

Several times he specifies the use of the interlocked queuing functions from
within the ISR. But these functions require a spinlock and the DDK says
“Callers of KeAcquireSpinlock must be running at IRQL <= DISPATCH_LEVEL”.

So, what am I missing?

Don Ward


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@careful.co.uk
To unsubscribe send a blank email to xxxxx@lists.osr.com

Actually, the insterlocked lists use the spin lock with their own model,
this will set the spin lock to protect against multiple processors and turn
off interrupts for requesting processor. This is discussed in Walter Oney’s
“Programming the Windows Driver Model” but is one of those bizarre items in
the DDK interfaces. The first time I read Walter’s book I had to walk
through the call at assembler level to confirm his statements.


Don Burn (MVP, Windows DDK)
Windows 2k/XP/2k3 Filesystem and Driver Consulting
Remove StopSpam from the email to reply

“Don Ward” wrote in message news:xxxxx@ntdev…
Thanks for the response; the mists begin to clear but I still don’t really
understand how it works.

There are two choices for the queue spinlock:
a) The Interrupt spinlock
b) some other spinlock

If I choose a), when the ISR uses ExInterlockedInsertHeadList, wouldn’t it
attempt to claim a spin lock it already has ?
I think option b) might deadlock on a Uniprocessor: Imagine the DPCforISR is
extracting stuff from the “done” queue when another interrupt occurs: the
ISR preempts it and spins waiting for the DPC to free the lock (which it
can’t because the ISR has preempted it by spinning).

Clearly that isn’t happening, otherwise everybody who has tried the “Extreme
DMA” would be complaining that it locks up sometimes. So something else,
something special, must be happening.

I haven’t actually tried any of this yet, I’m just trying to understand how
it all works before I start (an old-fashioned attitude, but it works for
me).

Regards

Don Ward

> -----Original Message-----
> From: xxxxx@lists.osr.com
> [mailto:xxxxx@lists.osr.com] On Behalf Of Don Burn
> Sent: 14 December 2004 20:28
> To: Windows System Software Devs Interest List
> Subject: Re:[ntdev] “Extreme DMA” and Interlocked queues
>
>
> Don,
>
> The Interlocked queues use the spinlock in a special
> way. This allows them to be called at any level up to
> interrupt level for the device. This is buried down in the
> documentation for these functions as:
>
> “If the caller uses only ExInterlocked…List routines to
> manipulate the list, these routines can be called from a
> single IRQL that is <= DIRQL. If other driver routines access
> the list using any other routines, such as the noninterlocked
> InsertHeadList, callers of ExInterlocked…List must be at <=
> DISPATCH_LEVEL”
>
>
> –
> Don Burn (MVP, Windows DDK)
> Windows 2k/XP/2k3 Filesystem and Driver Consulting
>
>
>
>
> “Don Ward” wrote in message
news:xxxxx@ntdev… As part of a new driver, I thought I’d have a crack at
PV’s “Extreme DMA” method, published in 2001. It looks, as he claims, fairly
straightforward but there is one question I can’t figure out:

Several times he specifies the use of the interlocked queuing functions from
within the ISR. But these functions require a spinlock and the DDK says
“Callers of KeAcquireSpinlock must be running at IRQL <= DISPATCH_LEVEL”.

So, what am I missing?

Don Ward


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@careful.co.uk
To unsubscribe send a blank email to xxxxx@lists.osr.com

> ----------

From: xxxxx@lists.osr.com[SMTP:xxxxx@lists.osr.com] on behalf of Don Ward[SMTP:xxxxx@careful.co.uk]
Reply To: Windows System Software Devs Interest List
Sent: Tuesday, December 14, 2004 9:51 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] “Extreme DMA” and Interlocked queues

I think option b) might deadlock on a Uniprocessor: Imagine the DPCforISR is
extracting stuff from the “done” queue when another interrupt occurs: the
ISR preempts it and spins waiting for the DPC to free the lock (which it
can’t because the ISR has preempted it by spinning).

That’s why interlocked routines use spinlock special way as Don wrote. If you disassembly one of routines, you’d notice it disables interrupts on current CPU to avoid above scenario. Note the deadlock isn’t limited to UP environment.

Best regards,

Michal Vodicka
UPEK, Inc.
[xxxxx@upek.com, http://www.upek.com]