Newbie - Memory - Is this safe?

I use the following code. Do I have to build an MDL and call
MmProbeAndLockPages if the current IRQL is PASSIVE_LEVEL? or will the
RtlZeroMemory cause all pages to be paged in before the IRQL is raised to
DISPATCH_LEVEL? The bottom line – is this safe?

KIRLQ oldIrql;
PVOID mem = NULL;
POOL_TYPE pt = PagedPool;

if (PASSIVE_LEVEL != KeGetCurrentIrql())
pt = NonPagedPool;

mem = ExAllocatePoolWithTag(pt, memSize, myTag);

RtlZeroMemory(mem, memSize);

__try
{
// Raise to DISPATCH_LEVEL
KeAcquireSpinLock(&myLock, &oldIrql);

// … use mem, etc…
}

__finally
{
// KeReleaseSpinLock(&myLock, oldIrql);

if (mem)
ExFreePool(mem);
}

> I use the following code. Do I have to build an MDL and call

MmProbeAndLockPages if the current IRQL is PASSIVE_LEVEL? or will the
RtlZeroMemory cause all pages to be paged in before the IRQL is raised to
DISPATCH_LEVEL? The bottom line – is this safe?

KIRLQ oldIrql;
PVOID mem = NULL;
POOL_TYPE pt = PagedPool;

if (PASSIVE_LEVEL != KeGetCurrentIrql())
pt = NonPagedPool;

mem = ExAllocatePoolWithTag(pt, memSize, myTag);

RtlZeroMemory(mem, memSize);

__try
{
// Raise to DISPATCH_LEVEL
KeAcquireSpinLock(&myLock, &oldIrql);

// … use mem, etc…
}

__finally
{
// KeReleaseSpinLock(&myLock, oldIrql);

if (mem)
ExFreePool(mem);
}

RtlZeroMemory is just a memset it does nothing to lock the memory in
place. For your example allocate the memory from NonPagedPool in either
case since
you are going to be referencing the memory at dispatch level.

Don Burn
Egenera, Inc

whether RtlZeroMemory manages to bring all the pages in is irrelevant.
unless you lock them down there’s nothing stopping the system from
invalidating them and paging them back out while you’re at dispatch
level.

if you want to touch data at dispatch level or above you MUST ensure it
is locked. Just hoping the pages are resident isn’t enough.

I’m not sure why you wouldn’t just always allocate non-paged pool in
this code snippet though. that seems like it would solve your problem
nicely.

-p

-----Original Message-----
From: Jon Anglin [mailto:xxxxx@fortres.com]
Sent: Monday, September 09, 2002 8:25 AM
To: NT Developers Interest List
Subject: [ntdev] Newbie - Memory - Is this safe?

I use the following code. Do I have to build an MDL and call
MmProbeAndLockPages if the current IRQL is PASSIVE_LEVEL? or will the
RtlZeroMemory cause all pages to be paged in before the IRQL is raised
to DISPATCH_LEVEL? The bottom line – is this safe?

KIRLQ oldIrql;
PVOID mem = NULL;
POOL_TYPE pt = PagedPool;

if (PASSIVE_LEVEL != KeGetCurrentIrql())
pt = NonPagedPool;

mem = ExAllocatePoolWithTag(pt, memSize, myTag);

RtlZeroMemory(mem, memSize);

__try
{
// Raise to DISPATCH_LEVEL
KeAcquireSpinLock(&myLock, &oldIrql);

// … use mem, etc…
}

__finally
{
// KeReleaseSpinLock(&myLock, oldIrql);

if (mem)
ExFreePool(mem);
}


You are currently subscribed to ntdev as: xxxxx@microsoft.com To
unsubscribe send a blank email to %%email.unsub%%