bugcheck irql_not_less_or_equal when acquire spinlock for bitmap

Hi,
In my code I am using Bitmap to maintain free node list.
I have allocated bimap buffer as a nonpaged pool.
I am protecting setting of bit in bit map using spinlock

code snippet-
KeAcquireSpinLock(&(context->ContextLock), &irql);

index = GIBMPFindFirstFreeBit(&(context->BitMap));

KeReleaseSpinLock(&(context->ContextLock), irql);

uint32
GIBMPFindFirstFreeBit(PRTL_BITMAP BitMap)
{
uint32 index;

ASSERT(NULL != BitMap);
index = RtlFindClearBitsAndSet(BitMap, 1, 0);

return index;
}

This code bugchecks with 0xa on driver unload.
However if I use guarded mutex instead of spinlock it works fine.

I suspect that Bitmap structure’s other elements might be paged.
Or is it that bitmap calls are sleeping calls ?

Thanks,
-Ulka

All of its memory is managed by what you allocate. No sleeping in bitmap
code. The Rtl bitmap routines are very simple. Look elsewhere in your code.
I have protected bitmaps with spinlocks with no issues.

On Thu, Dec 31, 2015 at 10:42 AM Ulka Vaze wrote:

> Hi,
> In my code I am using Bitmap to maintain free node list.
> I have allocated bimap buffer as a nonpaged pool.
> I am protecting setting of bit in bit map using spinlock
>
> code snippet-
> KeAcquireSpinLock(&(context->ContextLock), &irql);
>
> index = GIBMPFindFirstFreeBit(&(context->BitMap));
>
> KeReleaseSpinLock(&(context->ContextLock), irql);
>
>
> uint32
> GIBMPFindFirstFreeBit(PRTL_BITMAP BitMap)
> {
> uint32 index;
>
> ASSERT(NULL != BitMap);
> index = RtlFindClearBitsAndSet(BitMap, 1, 0);
>
> return index;
> }
>
> This code bugchecks with 0xa on driver unload.
> However if I use guarded mutex instead of spinlock it works fine.
>
> I suspect that Bitmap structure’s other elements might be paged.
> Or is it that bitmap calls are sleeping calls ?
>
> Thanks,
> -Ulka
>
> — NTDEV is sponsored by OSR Visit the list online at: MONTHLY seminars
> on crash dump analysis, WDF, Windows internals and software drivers!
> Details at To unsubscribe, visit the List Server section of OSR Online at

https://msdn.microsoft.com/en-us/library/windows/hardware/ff561874(v=vs.85).aspx
“Callers of RtlFindClearBitsAndSet must be running at IRQL <= APC_LEVEL if the memory that contains the bitmap variable is pageable or the memory at BitMapHeader is pageable. Otherwise, RtlFindClearBitsAndSet can be called at any IRQL.” - so if BitMap and BitMap->Buffer both nonpaged you can call it on aly IRQL. mistake in something else