IoCompletion routine

From DDK 3615:
“A driver’s IoCompletion routine executes in an arbitrary thread or DPC
context, and at an IRQL that is less than or equal to DISPATCH_LEVEL.
Because code written to execute at DISPATCH_LEVEL will also execute at
lower levels, IoCompletion routines should be designed for execution at
DISPATCH_LEVEL.”
“However, because these routines are not guaranteed to run at
DISPATCH_LEVEL, they must not call system routines that actually require
execution at DISPATCH_LEVEL.”

According to second quote and “executes in an arbitrary thread or DPC
context” in the first quote: must I not use KeRaiseIrql or
KeAcuireSpinLock in an IoCompletion routine? Does calling these routines
cause switching of stacks (I think which is not permitted in arbitrary
thread or DPC context)? Or can I use these sort of routines in
IoCompletion routine? Can anybody help me understand this context?

Thanks.
Hakim

You can certainly called KeRaiseIrql and KeAcquireSpinLock as long as you
restore before you leave the completion routine. The problem is that your
completion routine could possibly be called at IRQLs < DISPATCH_LEVEL (like
if the Driver completing the request did so from one of its dispatch
routines that was running at IRQL PASSIVE_LEVEL), so if you need to call
some routine who has a requirement of running at IRQL DISPATCH_LEVEL then
your going to have to check the current IRQL in your completion routine and
do the right thing.

–Mark


Mark Cariddi
Open Systems Resources, Inc.
www.osr.com
wrote in message news:xxxxx@ntdev…
>
> From DDK 3615:
> “A driver’s IoCompletion routine executes in an arbitrary thread or DPC
> context, and at an IRQL that is less than or equal to DISPATCH_LEVEL.
> Because code written to execute at DISPATCH_LEVEL will also execute at
> lower levels, IoCompletion routines should be designed for execution at
> DISPATCH_LEVEL.”
> “However, because these routines are not guaranteed to run at
> DISPATCH_LEVEL, they must not call system routines that actually require
> execution at DISPATCH_LEVEL.”
>
> According to second quote and “executes in an arbitrary thread or DPC
> context” in the first quote: must I not use KeRaiseIrql or
> KeAcuireSpinLock in an IoCompletion routine? Does calling these routines
> cause switching of stacks (I think which is not permitted in arbitrary
> thread or DPC context)? Or can I use these sort of routines in
> IoCompletion routine? Can anybody help me understand this context?
>
> Thanks.
> Hakim
>
>

If you require to be at DISPATCH to do something and you don’t know your
current IRQL, from a perf perspective it is much better to raise to irql
and not check the current irql then to check the current irql and raise
if you are lower then the desired level (this came straight from the NT
perf team during a perf review I was at).

d

-----Original Message-----
From: Mark J. Cariddi [mailto:xxxxx@osr.com]
Sent: Friday, July 26, 2002 12:11 PM
To: NT Developers Interest List
Subject: [ntdev] Re: IoCompletion routine

You can certainly called KeRaiseIrql and KeAcquireSpinLock as long as
you
restore before you leave the completion routine. The problem is that
your
completion routine could possibly be called at IRQLs < DISPATCH_LEVEL
(like if the Driver completing the request did so from one of its
dispatch routines that was running at IRQL PASSIVE_LEVEL), so if you
need to call some routine who has a requirement of running at IRQL
DISPATCH_LEVEL then your going to have to check the current IRQL in your
completion routine and do the right thing.

–Mark


Mark Cariddi
Open Systems Resources, Inc.
www.osr.com
wrote in message news:xxxxx@ntdev…
>
> From DDK 3615:
> “A driver’s IoCompletion routine executes in an arbitrary thread or
> DPC context, and at an IRQL that is less than or equal to
> DISPATCH_LEVEL. Because code written to execute at DISPATCH_LEVEL will

> also execute at lower levels, IoCompletion routines should be designed

> for execution at DISPATCH_LEVEL.” “However, because these routines are

> not guaranteed to run at DISPATCH_LEVEL, they must not call system
> routines that actually require execution at DISPATCH_LEVEL.”
>
> According to second quote and “executes in an arbitrary thread or DPC
> context” in the first quote: must I not use KeRaiseIrql or
> KeAcuireSpinLock in an IoCompletion routine? Does calling these
> routines cause switching of stacks (I think which is not permitted in
> arbitrary thread or DPC context)? Or can I use these sort of routines
> in IoCompletion routine? Can anybody help me understand this context?
>
> Thanks.
> Hakim
>
>


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

Doron,

I dont understand this right … should I call KeRaiseIrql(Level A) , then
check if IRQL if under level A , and if its not raise again to same level A
? Doesnt make any sense to me.
I think I missunderstand what you wanna say. Please explain.

Dan

“Doron Holan” wrote in message
news:xxxxx@ntdev…

If you require to be at DISPATCH to do something and you don’t know your
current IRQL, from a perf perspective it is much better to raise to irql
and not check the current irql then to check the current irql and raise
if you are lower then the desired level (this came straight from the NT
perf team during a perf review I was at).

d

-----Original Message-----
From: Mark J. Cariddi [mailto:xxxxx@osr.com]
Sent: Friday, July 26, 2002 12:11 PM
To: NT Developers Interest List
Subject: [ntdev] Re: IoCompletion routine

You can certainly called KeRaiseIrql and KeAcquireSpinLock as long as
you
restore before you leave the completion routine. The problem is that
your
completion routine could possibly be called at IRQLs < DISPATCH_LEVEL
(like if the Driver completing the request did so from one of its
dispatch routines that was running at IRQL PASSIVE_LEVEL), so if you
need to call some routine who has a requirement of running at IRQL
DISPATCH_LEVEL then your going to have to check the current IRQL in your
completion routine and do the right thing.

–Mark


Mark Cariddi
Open Systems Resources, Inc.
www.osr.com
wrote in message news:xxxxx@ntdev…
>
> From DDK 3615:
> “A driver’s IoCompletion routine executes in an arbitrary thread or
> DPC context, and at an IRQL that is less than or equal to
> DISPATCH_LEVEL. Because code written to execute at DISPATCH_LEVEL will

> also execute at lower levels, IoCompletion routines should be designed

> for execution at DISPATCH_LEVEL.” “However, because these routines are

> not guaranteed to run at DISPATCH_LEVEL, they must not call system
> routines that actually require execution at DISPATCH_LEVEL.”
>
> According to second quote and “executes in an arbitrary thread or DPC
> context” in the first quote: must I not use KeRaiseIrql or
> KeAcuireSpinLock in an IoCompletion routine? Does calling these
> routines cause switching of stacks (I think which is not permitted in
> arbitrary thread or DPC context)? Or can I use these sort of routines
> in IoCompletion routine? Can anybody help me understand this context?
>
> Thanks.
> Hakim
>
>


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

there are two options in your completion routine. Note these aren’

if(KeGetCurrentIrql() < DISPATCH_LEVEL) {
KeRaiseIrql(DISPATCH_LEVEL, &oldIrql);
}

… do work …

if(oldIrql < DISPATCH_LEVEL) {
KeLowerIrql(oldIrql);
}

KeRaiseIrql(DISPATCH_LEVEL, &oldIrql);

… do work …

KeLowerIrql(oldIrql);

I believe doron is saying that option 2 is the better one - the cost of
checking current IRQL in option 1 outweighs the savings gained by not
doing the raise & lower.

-p

-----Original Message-----
From: Dan Partelly [mailto:xxxxx@rdsor.ro]
Sent: Friday, July 26, 2002 4:51 PM
To: NT Developers Interest List
Subject: [ntdev] Re: IoCompletion routine

Doron,

I dont understand this right … should I call KeRaiseIrql(Level A) ,
then check if IRQL if under level A , and if its not raise again to same
level A ? Doesnt make any sense to me. I think I missunderstand what you
wanna say. Please explain.

Dan

“Doron Holan” wrote in message
news:xxxxx@ntdev…

If you require to be at DISPATCH to do something and you don’t know your
current IRQL, from a perf perspective it is much better to raise to irql
and not check the current irql then to check the current irql and raise
if you are lower then the desired level (this came straight from the NT
perf team during a perf review I was at).

d

-----Original Message-----
From: Mark J. Cariddi [mailto:xxxxx@osr.com]
Sent: Friday, July 26, 2002 12:11 PM
To: NT Developers Interest List
Subject: [ntdev] Re: IoCompletion routine

You can certainly called KeRaiseIrql and KeAcquireSpinLock as long as
you
restore before you leave the completion routine. The problem is that
your
completion routine could possibly be called at IRQLs < DISPATCH_LEVEL
(like if the Driver completing the request did so from one of its
dispatch routines that was running at IRQL PASSIVE_LEVEL), so if you
need to call some routine who has a requirement of running at IRQL
DISPATCH_LEVEL then your going to have to check the current IRQL in your
completion routine and do the right thing.

–Mark


Mark Cariddi
Open Systems Resources, Inc.
www.osr.com
wrote in message news:xxxxx@ntdev…
>
> From DDK 3615:
> “A driver’s IoCompletion routine executes in an arbitrary thread or
> DPC context, and at an IRQL that is less than or equal to
> DISPATCH_LEVEL. Because code written to execute at DISPATCH_LEVEL will

> also execute at lower levels, IoCompletion routines should be designed

> for execution at DISPATCH_LEVEL.” “However, because these routines are

> not guaranteed to run at DISPATCH_LEVEL, they must not call system
> routines that actually require execution at DISPATCH_LEVEL.”
>
> According to second quote and “executes in an arbitrary thread or DPC
> context” in the first quote: must I not use KeRaiseIrql or
> KeAcuireSpinLock in an IoCompletion routine? Does calling these
> routines cause switching of stacks (I think which is not permitted in
> arbitrary thread or DPC context)? Or can I use these sort of routines
> in IoCompletion routine? Can anybody help me understand this context?
>
> Thanks.
> Hakim
>
>


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


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

Yes, this make sense. I bleleive I completly missed what he exprimed.

Thx, Dan

“Peter Wieland” wrote in message
news:xxxxx@ntdev…

there are two options in your completion routine. Note these aren’

1)

if(KeGetCurrentIrql() < DISPATCH_LEVEL) {
KeRaiseIrql(DISPATCH_LEVEL, &oldIrql);
}

… do work …

if(oldIrql < DISPATCH_LEVEL) {
KeLowerIrql(oldIrql);
}

2)

KeRaiseIrql(DISPATCH_LEVEL, &oldIrql);

… do work …

KeLowerIrql(oldIrql);

I believe doron is saying that option 2 is the better one - the cost of
checking current IRQL in option 1 outweighs the savings gained by not
doing the raise & lower.

-p

-----Original Message-----
From: Dan Partelly [mailto:xxxxx@rdsor.ro]
Sent: Friday, July 26, 2002 4:51 PM
To: NT Developers Interest List
Subject: [ntdev] Re: IoCompletion routine

Doron,

I dont understand this right … should I call KeRaiseIrql(Level A) ,
then check if IRQL if under level A , and if its not raise again to same
level A ? Doesnt make any sense to me. I think I missunderstand what you
wanna say. Please explain.

Dan

“Doron Holan” wrote in message
news:xxxxx@ntdev…

If you require to be at DISPATCH to do something and you don’t know your
current IRQL, from a perf perspective it is much better to raise to irql
and not check the current irql then to check the current irql and raise
if you are lower then the desired level (this came straight from the NT
perf team during a perf review I was at).

d

-----Original Message-----
From: Mark J. Cariddi [mailto:xxxxx@osr.com]
Sent: Friday, July 26, 2002 12:11 PM
To: NT Developers Interest List
Subject: [ntdev] Re: IoCompletion routine

You can certainly called KeRaiseIrql and KeAcquireSpinLock as long as
you
restore before you leave the completion routine. The problem is that
your
completion routine could possibly be called at IRQLs < DISPATCH_LEVEL
(like if the Driver completing the request did so from one of its
dispatch routines that was running at IRQL PASSIVE_LEVEL), so if you
need to call some routine who has a requirement of running at IRQL
DISPATCH_LEVEL then your going to have to check the current IRQL in your
completion routine and do the right thing.

–Mark


Mark Cariddi
Open Systems Resources, Inc.
www.osr.com
wrote in message news:xxxxx@ntdev…
>
> From DDK 3615:
> “A driver’s IoCompletion routine executes in an arbitrary thread or
> DPC context, and at an IRQL that is less than or equal to
> DISPATCH_LEVEL. Because code written to execute at DISPATCH_LEVEL will

> also execute at lower levels, IoCompletion routines should be designed

> for execution at DISPATCH_LEVEL.” “However, because these routines are

> not guaranteed to run at DISPATCH_LEVEL, they must not call system
> routines that actually require execution at DISPATCH_LEVEL.”
>
> According to second quote and “executes in an arbitrary thread or DPC
> context” in the first quote: must I not use KeRaiseIrql or
> KeAcuireSpinLock in an IoCompletion routine? Does calling these
> routines cause switching of stacks (I think which is not permitted in
> arbitrary thread or DPC context)? Or can I use these sort of routines
> in IoCompletion routine? Can anybody help me understand this context?
>
> Thanks.
> Hakim
>
>


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


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

> According to second quote and "executes in an arbitrary thread or
DPC

context" in the first quote: must I not use KeRaiseIrql or
KeAcuireSpinLock in an IoCompletion routine?

Why not?
Usually, there are small number of cases when you will want to use
KeRaiseIrql. KeAcquireSpinLock can be used for sure.

Be careful to never calling IoCompleteRequest while holding a
spinlock.

Max

Thanks all who have made contribution to my question. I like NtDev.

Hakim

> Be careful to never calling IoCompleteRequest while holding a

spinlock.

I also found in the archive (strongly recommended) not to call
IocallDriver while holding a spinlock. But what about IoFreeIrp or
IoCancelIrp? Any idea?

Thanks,
Hakim

> According to second quote and “executes in an arbitrary thread or
DPC
> context” in the first quote: must I not use KeRaiseIrql or
> KeAcuireSpinLock in an IoCompletion routine?

Why not?
Usually, there are small number of cases when you will want to use
KeRaiseIrql. KeAcquireSpinLock can be used for sure.

Be careful to never calling IoCompleteRequest while holding a
spinlock.

Max

Yes, peter clarified it correctly.

D

This posting is provided “AS IS” with no warranties, and confers no
rights.
-----Original Message-----
From: Peter Wieland
Sent: Friday, July 26, 2002 5:15 PM
To: NT Developers Interest List
Subject: [ntdev] Re: IoCompletion routine

there are two options in your completion routine. Note these aren’

if(KeGetCurrentIrql() < DISPATCH_LEVEL) {
KeRaiseIrql(DISPATCH_LEVEL, &oldIrql);
}

… do work …

if(oldIrql < DISPATCH_LEVEL) {
KeLowerIrql(oldIrql);
}

KeRaiseIrql(DISPATCH_LEVEL, &oldIrql);

… do work …

KeLowerIrql(oldIrql);

I believe doron is saying that option 2 is the better one - the cost of
checking current IRQL in option 1 outweighs the savings gained by not
doing the raise & lower.

-p

-----Original Message-----
From: Dan Partelly [mailto:xxxxx@rdsor.ro]
Sent: Friday, July 26, 2002 4:51 PM
To: NT Developers Interest List
Subject: [ntdev] Re: IoCompletion routine

Doron,

I dont understand this right … should I call KeRaiseIrql(Level A) ,
then check if IRQL if under level A , and if its not raise again to same
level A ? Doesnt make any sense to me. I think I missunderstand what you
wanna say. Please explain.

Dan

“Doron Holan” wrote in message
news:xxxxx@ntdev…

If you require to be at DISPATCH to do something and you don’t know your
current IRQL, from a perf perspective it is much better to raise to irql
and not check the current irql then to check the current irql and raise
if you are lower then the desired level (this came straight from the NT
perf team during a perf review I was at).

d

-----Original Message-----
From: Mark J. Cariddi [mailto:xxxxx@osr.com]
Sent: Friday, July 26, 2002 12:11 PM
To: NT Developers Interest List
Subject: [ntdev] Re: IoCompletion routine

You can certainly called KeRaiseIrql and KeAcquireSpinLock as long as
you
restore before you leave the completion routine. The problem is that
your
completion routine could possibly be called at IRQLs < DISPATCH_LEVEL
(like if the Driver completing the request did so from one of its
dispatch routines that was running at IRQL PASSIVE_LEVEL), so if you
need to call some routine who has a requirement of running at IRQL
DISPATCH_LEVEL then your going to have to check the current IRQL in your
completion routine and do the right thing.

–Mark


Mark Cariddi
Open Systems Resources, Inc.
www.osr.com
wrote in message news:xxxxx@ntdev…
>
> From DDK 3615:
> “A driver’s IoCompletion routine executes in an arbitrary thread or
> DPC context, and at an IRQL that is less than or equal to
> DISPATCH_LEVEL. Because code written to execute at DISPATCH_LEVEL will

> also execute at lower levels, IoCompletion routines should be designed

> for execution at DISPATCH_LEVEL.” “However, because these routines are

> not guaranteed to run at DISPATCH_LEVEL, they must not call system
> routines that actually require execution at DISPATCH_LEVEL.”
>
> According to second quote and “executes in an arbitrary thread or DPC
> context” in the first quote: must I not use KeRaiseIrql or
> KeAcuireSpinLock in an IoCompletion routine? Does calling these
> routines cause switching of stacks (I think which is not permitted in
> arbitrary thread or DPC context)? Or can I use these sort of routines
> in IoCompletion routine? Can anybody help me understand this context?
>
> Thanks.
> Hakim
>
>


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


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


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

It might be good to mention that the only reason you can call
KeRaiseIrql in this instance without checking the current Irql
is that the IoCompletion is garaunteed to be running at
DISPATCH_LEVEL or lower. If it weren’t, you could mistakenly
bugcheck.

-Jeff

-----Original Message-----
From: Doron Holan [mailto:xxxxx@windows.microsoft.com]
Sent: Monday, July 29, 2002 4:24 PM
To: NT Developers Interest List
Subject: [ntdev] Re: IoCompletion routine

Yes, peter clarified it correctly.

D

This posting is provided “AS IS” with no warranties, and confers no
rights.
-----Original Message-----
From: Peter Wieland
Sent: Friday, July 26, 2002 5:15 PM
To: NT Developers Interest List
Subject: [ntdev] Re: IoCompletion routine

there are two options in your completion routine. Note these aren’

if(KeGetCurrentIrql() < DISPATCH_LEVEL) {
KeRaiseIrql(DISPATCH_LEVEL, &oldIrql);
}

… do work …

if(oldIrql < DISPATCH_LEVEL) {
KeLowerIrql(oldIrql);
}

KeRaiseIrql(DISPATCH_LEVEL, &oldIrql);

… do work …

KeLowerIrql(oldIrql);

I believe doron is saying that option 2 is the better one - the cost of
checking current IRQL in option 1 outweighs the savings gained by not
doing the raise & lower.

-p

-----Original Message-----
From: Dan Partelly [mailto:xxxxx@rdsor.ro]
Sent: Friday, July 26, 2002 4:51 PM
To: NT Developers Interest List
Subject: [ntdev] Re: IoCompletion routine

Doron,

I dont understand this right … should I call KeRaiseIrql(Level A) ,
then check if IRQL if under level A , and if its not raise again to same
level A ? Doesnt make any sense to me. I think I missunderstand what you
wanna say. Please explain.

Dan

“Doron Holan” wrote in message
news:xxxxx@ntdev…

If you require to be at DISPATCH to do something and you don’t know your
current IRQL, from a perf perspective it is much better to raise to irql
and not check the current irql then to check the current irql and raise
if you are lower then the desired level (this came straight from the NT
perf team during a perf review I was at).

d

-----Original Message-----
From: Mark J. Cariddi [mailto:xxxxx@osr.com]
Sent: Friday, July 26, 2002 12:11 PM
To: NT Developers Interest List
Subject: [ntdev] Re: IoCompletion routine

You can certainly called KeRaiseIrql and KeAcquireSpinLock as long as
you
restore before you leave the completion routine. The problem is that
your
completion routine could possibly be called at IRQLs < DISPATCH_LEVEL
(like if the Driver completing the request did so from one of its
dispatch routines that was running at IRQL PASSIVE_LEVEL), so if you
need to call some routine who has a requirement of running at IRQL
DISPATCH_LEVEL then your going to have to check the current IRQL in your
completion routine and do the right thing.

–Mark


Mark Cariddi
Open Systems Resources, Inc.
www.osr.com
wrote in message news:xxxxx@ntdev…
>
> From DDK 3615:
> “A driver’s IoCompletion routine executes in an arbitrary thread or
> DPC context, and at an IRQL that is less than or equal to
> DISPATCH_LEVEL. Because code written to execute at DISPATCH_LEVEL will

> also execute at lower levels, IoCompletion routines should be designed

> for execution at DISPATCH_LEVEL.” “However, because these routines are

> not guaranteed to run at DISPATCH_LEVEL, they must not call system
> routines that actually require execution at DISPATCH_LEVEL.”
>
> According to second quote and “executes in an arbitrary thread or DPC
> context” in the first quote: must I not use KeRaiseIrql or
> KeAcuireSpinLock in an IoCompletion routine? Does calling these
> routines cause switching of stacks (I think which is not permitted in
> arbitrary thread or DPC context)? Or can I use these sort of routines
> in IoCompletion routine? Can anybody help me understand this context?
>
> Thanks.
> Hakim
>
>


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


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


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


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


This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
the latest virus scan software available for the presence of computer
viruses.

IoFreeIrp is possibly OK, since it will not call anything complex.
As about IoCancelIrp - no-no, you will not want underlying cancel
routine to run with a spinlock held.

Max

----- Original Message -----
From:
To: “NT Developers Interest List”
Sent: Monday, July 29, 2002 7:12 PM
Subject: [ntdev] Re: IoCompletion routine

> > Be careful to never calling IoCompleteRequest while holding a
> > spinlock.
>
> I also found in the archive (strongly recommended) not to call
> IocallDriver while holding a spinlock. But what about IoFreeIrp or
> IoCancelIrp? Any idea?
>
> Thanks,
> Hakim
>
>
> > > According to second quote and “executes in an arbitrary thread
or
> > DPC
> > > context” in the first quote: must I not use KeRaiseIrql or
> > > KeAcuireSpinLock in an IoCompletion routine?
> >
> > Why not?
> > Usually, there are small number of cases when you will want to use
> > KeRaiseIrql. KeAcquireSpinLock can be used for sure.
> >
> > Be careful to never calling IoCompleteRequest while holding a
> > spinlock.
> >
> > Max
>
> —
> You are currently subscribed to ntdev as: xxxxx@storagecraft.com
> To unsubscribe send a blank email to %%email.unsub%%
>