Wow . “conflating” . had to look that one up . to bring together, to meld or
fuse, two or more things into a unified whole, which can lead to fallacious
reasoning if care is not taken.J
In my case, thinking spinlock always gives me a headache, and thinking
MULTIPLE spinlocks simply gives me SUPER MULTIPLE headaches, and I’m out of
aspirin in the house. I can see Marks point that there may be occasions
where more efficient code may lead to out of order release, but for the sake
of my own sanity, I most likely will always release by inverse order. Were
it an order of magnitude more efficient for either memory or processing
time, then I might consider it, but not to save one or two lines of code. It
just isn’t worth the extra time needed to get it right, nor the trip to the
pharmacy for the aspirin.
Gary G. Little
H (952) 223-1349
C (952) 454-4629
xxxxx@comcast.net
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Mark Roddy
Sent: Thursday, August 26, 2010 7:43 AM
To: Windows System Software Devs Interest List
Subject: Re: [ntdev] releasing a spinlock when not at dispatch level
You only get to that deadlock by conflating IRQL state management with
spinlocks. As I said from the outset, you have to maintain the correct IRQL
state. That state is determined by the first lock you acquire in a sequence
of lock acquisitions and must be restored on the release of the LAST lock
released.
So again, after accounting for correct IRQL management, what deadlock
potential exists on release?
Mark Roddy
On Thu, Aug 26, 2010 at 12:17 AM, Jan Bottorff
wrote:
I’m going to side with the camp that says locks should be released in the
inverse order they are acquired, using the below message as a problem case.
You can certainly come up with trivial cases where this is not a
requirement, but there also are cases where it is.
So the lock hierarchy is spinllock1 then spinlock2, and as long as you
always take ownership and release ownership of the locks in the same order
you supposed to be are ok. But in the below code, suppose after you call
KeReleaseSpinLock(&spinlock1, Irql1);, which returns you to PASSIVE_LEVEL,
your thread is context switched and some other code tried to follow the same
lock hierarchy. It may deadlock because it raises IRQL to DISPATCH_LEVEL by
acquiring spinlock1, and can never acquire spinlock2, because it’s already
held in the other thread. On a single processor system, you have just
guaranteed a system deadlock, even though no individual function violated
the take ownership rules. What was violated was the paring of spinlocks with
their IRQL.
The WDK docs DO actually talk about lock release order at
“http://msdn.microsoft.com/en-us/library/ms810047.aspx” file .doc file on
“Driver Lifecycle Fundamentals->Key Driver Concepts->Lock, Deadlocks, and
Synchronization” at http://www.microsoft.com/whdc/driver/kernel/locks.mspx
has these same words.
It says “Similarly, a code sequence that uses multiple locks should release
them in the inverse of the order in which it acquired them. That is, it
should release the most recently acquired lock first.”
As the title of this message thread says “releasing a spinlock when not at
dispatch level”, the docs for KeReleaseSpinLock says “Callers of this
routine are running at IRQL = DISPATCH_LEVEL”, which would be violated if
you tried to release it anything except DISPATCH_LEVEL. The code below is
also likely invalid for this reason, as spinlock2 will need to get unlocked
eventually, and you are already at < DISPATCH_LEVEL.
Jan
>
> Consider the following scenario:
>
> KIRQL irql1,irql2;
>
> KeAcquireSpinLock(&spinlock1, &irql1);
>
> KeAcquireSpinLock(&spinlock2, &irql2);
>
>
> …
>
>
>
> KeReleaseSpinLock(&spinlock1, Irql1);
>
>
> Assuming that the original call was made at IRQL< DPC_LEVEL the above line
> will set IRQL to its original level, despite the fact that spinlock2 is
still being
> held…
—
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