DPC query

Hi,

I come from Linux background and trying to understand Windows internals.

I am reading Inside Microsoft Windows 2000 by David Solomon.

I have a query about DPC. The book specified has something like this about DPC.

"Dispatch or deferred procedure call (DPC) interrupts When a thread
can no longer continue executing, perhaps because it has terminated or
because it voluntarily enters a wait state, the kernel calls the
dispatcher directly to effect an immediate context switch. Sometimes,
however, the kernel detects that rescheduling should occur when it is
deep within many layers of code. In this situation, the ideal solution
is to request dispatching but defer its occurrence until the kernel
completes its current activity. Using a DPC software interrupt is a
convenient way to achieve this delay. "

It says that DPC’s defer the rescheduling of a process.

My questions are:

On what conditions we defer the rescheduling?

if we defer the rescheduling and if the current process blocks on

any object wont the CPU stay idle ?

Waiting for the response.

Thanks,
Vinay

A DPC defers scheduling of the process while the DPC is running in that
process context. The DPC is not allowed to block on anything other than
spinlocks.

=====================
Mark Roddy DDK MVP
Windows 2003/XP/2000 Consulting
Hollis Technology Solutions 603-321-1032
www.hollistech.com

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Vinay Kalkoti
Sent: Thursday, March 30, 2006 6:26 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] DPC query

Hi,

I come from Linux background and trying to understand Windows
internals.

I am reading Inside Microsoft Windows 2000 by David Solomon.

I have a query about DPC. The book specified has something
like this about DPC.

"Dispatch or deferred procedure call (DPC) interrupts When a
thread can no longer continue executing, perhaps because it
has terminated or because it voluntarily enters a wait state,
the kernel calls the dispatcher directly to effect an
immediate context switch. Sometimes, however, the kernel
detects that rescheduling should occur when it is deep within
many layers of code. In this situation, the ideal solution is
to request dispatching but defer its occurrence until the
kernel completes its current activity. Using a DPC software
interrupt is a convenient way to achieve this delay. "

It says that DPC’s defer the rescheduling of a process.

My questions are:

On what conditions we defer the rescheduling?

if we defer the rescheduling and if the current process

blocks on any object wont the CPU stay idle ?

Waiting for the response.

Thanks,
Vinay


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

To unsubscribe, visit the List Server section of OSR Online
at http://www.osronline.com/page.cfm?name=ListServer

A DPC runs at a level where preemption is disabled on the processor
where it is running. One instance of a DPC is an interrupt DPC and would
be analogous to a Linux “bottom-half” interrupt (is that what they still
call it?). It can also be used to run a function to its entirety without
being preempted (I think Linux has a concept of FIFO scheduling - this
would be somewhat similar. The driver writer controls the queueing of a
DPC and they generally get run in FIFO order from the processor’s
queue.).

Blocking is not allowed in a DPC call. Data is protectected with a
spinlock on SMP systems in case something on the other processortries to
access the same data. On single processor systems, only device
interrupts (linux “top-half” interrupts) can preempt a DPC.

Beverly

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Vinay Kalkoti
Sent: Thursday, March 30, 2006 6:26 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] DPC query

Hi,

I come from Linux background and trying to understand Windows internals.

I am reading Inside Microsoft Windows 2000 by David Solomon.

I have a query about DPC. The book specified has something like this
about DPC.

"Dispatch or deferred procedure call (DPC) interrupts When a thread can
no longer continue executing, perhaps because it has terminated or
because it voluntarily enters a wait state, the kernel calls the
dispatcher directly to effect an immediate context switch. Sometimes,
however, the kernel detects that rescheduling should occur when it is
deep within many layers of code. In this situation, the ideal solution
is to request dispatching but defer its occurrence until the kernel
completes its current activity. Using a DPC software interrupt is a
convenient way to achieve this delay. "

It says that DPC’s defer the rescheduling of a process.

My questions are:

On what conditions we defer the rescheduling?

if we defer the rescheduling and if the current process blocks on any

object wont the CPU stay idle ?

Waiting for the response.

Thanks,
Vinay


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

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

A few notes on beverly’s explaination:

There are two related concepts here - DPCs and interrupt level.
Interrupt level controls what can preempt the code currently running on
the processor and whether the thread can be rescheduled. Code running
at dispatch-level can still be preempted by a higher prioirty interrupt
(like a device interrupt) but will not get rescheduled.

You can think of a DPC as a way to inject a dispatch-level interrupt
into a processor. The DPC routine is sort of like a custom ISR. Since
the DPC routine runs at dispatch level it can still be pre-empted but
the thread it interrupted will not be rescheduled.

Also, unless you’re really ready to take on writing two drivers (one for
UP and one for MP), you should just ignore the system type and always
synchronize your data properly.

-p

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Brown, Beverly
Sent: Thursday, March 30, 2006 5:52 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] DPC query

A DPC runs at a level where preemption is disabled on the processor
where it is running. One instance of a DPC is an interrupt DPC and would
be analogous to a Linux “bottom-half” interrupt (is that what they still
call it?). It can also be used to run a function to its entirety without
being preempted (I think Linux has a concept of FIFO scheduling - this
would be somewhat similar. The driver writer controls the queueing of a
DPC and they generally get run in FIFO order from the processor’s
queue.).

Blocking is not allowed in a DPC call. Data is protectected with a
spinlock on SMP systems in case something on the other processortries to
access the same data. On single processor systems, only device
interrupts (linux “top-half” interrupts) can preempt a DPC.

Beverly

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Vinay Kalkoti
Sent: Thursday, March 30, 2006 6:26 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] DPC query

Hi,

I come from Linux background and trying to understand Windows internals.

I am reading Inside Microsoft Windows 2000 by David Solomon.

I have a query about DPC. The book specified has something like this
about DPC.

"Dispatch or deferred procedure call (DPC) interrupts When a thread can
no longer continue executing, perhaps because it has terminated or
because it voluntarily enters a wait state, the kernel calls the
dispatcher directly to effect an immediate context switch. Sometimes,
however, the kernel detects that rescheduling should occur when it is
deep within many layers of code. In this situation, the ideal solution
is to request dispatching but defer its occurrence until the kernel
completes its current activity. Using a DPC software interrupt is a
convenient way to achieve this delay. "

It says that DPC’s defer the rescheduling of a process.

My questions are:

On what conditions we defer the rescheduling?

if we defer the rescheduling and if the current process blocks on any

object wont the CPU stay idle ?

Waiting for the response.

Thanks,
Vinay


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

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


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

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

The use of different drivers for UP and MP probably wouldn’t work well in
any case. It might if they were dynamically loaded or based upon wether the
OS kernel is a UP or SMP edition. Otherwise someone could have the /onecpu
switch in the boot.ini on one boot option and not the other. If it had the
kernel & hal boot.ini switches you could have a single system boot in UP and
SMP mode as desired and have the UP and SMP versions of the kernel too.
Don’t know why you would want it, but someone would try it.

“Peter Wieland” wrote in message
news:xxxxx@ntdev…
A few notes on beverly’s explaination:

There are two related concepts here - DPCs and interrupt level.
Interrupt level controls what can preempt the code currently running on
the processor and whether the thread can be rescheduled. Code running
at dispatch-level can still be preempted by a higher prioirty interrupt
(like a device interrupt) but will not get rescheduled.

You can think of a DPC as a way to inject a dispatch-level interrupt
into a processor. The DPC routine is sort of like a custom ISR. Since
the DPC routine runs at dispatch level it can still be pre-empted but
the thread it interrupted will not be rescheduled.

Also, unless you’re really ready to take on writing two drivers (one for
UP and one for MP), you should just ignore the system type and always
synchronize your data properly.

-p

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Brown, Beverly
Sent: Thursday, March 30, 2006 5:52 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] DPC query

A DPC runs at a level where preemption is disabled on the processor
where it is running. One instance of a DPC is an interrupt DPC and would
be analogous to a Linux “bottom-half” interrupt (is that what they still
call it?). It can also be used to run a function to its entirety without
being preempted (I think Linux has a concept of FIFO scheduling - this
would be somewhat similar. The driver writer controls the queueing of a
DPC and they generally get run in FIFO order from the processor’s
queue.).

Blocking is not allowed in a DPC call. Data is protectected with a
spinlock on SMP systems in case something on the other processortries to
access the same data. On single processor systems, only device
interrupts (linux “top-half” interrupts) can preempt a DPC.

Beverly

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Vinay Kalkoti
Sent: Thursday, March 30, 2006 6:26 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] DPC query

Hi,

I come from Linux background and trying to understand Windows internals.

I am reading Inside Microsoft Windows 2000 by David Solomon.

I have a query about DPC. The book specified has something like this
about DPC.

"Dispatch or deferred procedure call (DPC) interrupts When a thread can
no longer continue executing, perhaps because it has terminated or
because it voluntarily enters a wait state, the kernel calls the
dispatcher directly to effect an immediate context switch. Sometimes,
however, the kernel detects that rescheduling should occur when it is
deep within many layers of code. In this situation, the ideal solution
is to request dispatching but defer its occurrence until the kernel
completes its current activity. Using a DPC software interrupt is a
convenient way to achieve this delay. "

It says that DPC’s defer the rescheduling of a process.

My questions are:
# On what conditions we defer the rescheduling?
# if we defer the rescheduling and if the current process blocks on any
object wont the CPU stay idle ?

Waiting for the response.

Thanks,
Vinay


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

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


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

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

Snippet from below: “Since the DPC routine runs at dispatch level it can
still be pre-empted but the thread it interrupted will not be
rescheduled.” Which thread do you mean here ? I assume in this case it
is DPC. Right ?

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Peter Wieland
Sent: Thursday, March 30, 2006 8:15 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] DPC query

A few notes on beverly’s explaination:

There are two related concepts here - DPCs and interrupt level.
Interrupt level controls what can preempt the code currently running on
the processor and whether the thread can be rescheduled. Code running
at dispatch-level can still be preempted by a higher prioirty interrupt
(like a device interrupt) but will not get rescheduled.

You can think of a DPC as a way to inject a dispatch-level interrupt
into a processor. The DPC routine is sort of like a custom ISR. Since
the DPC routine runs at dispatch level it can still be pre-empted but
the thread it interrupted will not be rescheduled.

Also, unless you’re really ready to take on writing two drivers (one for
UP and one for MP), you should just ignore the system type and always
synchronize your data properly.

-p

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Brown, Beverly
Sent: Thursday, March 30, 2006 5:52 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] DPC query

A DPC runs at a level where preemption is disabled on the processor
where it is running. One instance of a DPC is an interrupt DPC and would
be analogous to a Linux “bottom-half” interrupt (is that what they still
call it?). It can also be used to run a function to its entirety without
being preempted (I think Linux has a concept of FIFO scheduling - this
would be somewhat similar. The driver writer controls the queueing of a
DPC and they generally get run in FIFO order from the processor’s
queue.).

Blocking is not allowed in a DPC call. Data is protectected with a
spinlock on SMP systems in case something on the other processortries to
access the same data. On single processor systems, only device
interrupts (linux “top-half” interrupts) can preempt a DPC.

Beverly

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Vinay Kalkoti
Sent: Thursday, March 30, 2006 6:26 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] DPC query

Hi,

I come from Linux background and trying to understand Windows internals.

I am reading Inside Microsoft Windows 2000 by David Solomon.

I have a query about DPC. The book specified has something like this
about DPC.

"Dispatch or deferred procedure call (DPC) interrupts When a thread can
no longer continue executing, perhaps because it has terminated or
because it voluntarily enters a wait state, the kernel calls the
dispatcher directly to effect an immediate context switch. Sometimes,
however, the kernel detects that rescheduling should occur when it is
deep within many layers of code. In this situation, the ideal solution
is to request dispatching but defer its occurrence until the kernel
completes its current activity. Using a DPC software interrupt is a
convenient way to achieve this delay. "

It says that DPC’s defer the rescheduling of a process.

My questions are:

On what conditions we defer the rescheduling?

if we defer the rescheduling and if the current process blocks on any

object wont the CPU stay idle ?

Waiting for the response.

Thanks,
Vinay


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

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


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

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


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

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

Every processor in the system is running some thread at passive level.
It might just be running the idle thread (which halts, and isn’t really
a thread, but ignore the magic and pretend it’s a thread). Threads are
what get scheduled.

DPCs interrupt threads and when they do the thread they interrupt is
pinned to that processor and cannot run anywhere else until they exit
and the IRQL drops back down below dispatch level.

-p

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Narayanasamy,
Raja M
Sent: Thursday, March 30, 2006 9:48 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] DPC query

Snippet from below: “Since the DPC routine runs at dispatch level it can
still be pre-empted but the thread it interrupted will not be
rescheduled.” Which thread do you mean here ? I assume in this case it
is DPC. Right ?

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Peter Wieland
Sent: Thursday, March 30, 2006 8:15 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] DPC query

A few notes on beverly’s explaination:

There are two related concepts here - DPCs and interrupt level.
Interrupt level controls what can preempt the code currently running on
the processor and whether the thread can be rescheduled. Code running
at dispatch-level can still be preempted by a higher prioirty interrupt
(like a device interrupt) but will not get rescheduled.

You can think of a DPC as a way to inject a dispatch-level interrupt
into a processor. The DPC routine is sort of like a custom ISR. Since
the DPC routine runs at dispatch level it can still be pre-empted but
the thread it interrupted will not be rescheduled.

Also, unless you’re really ready to take on writing two drivers (one for
UP and one for MP), you should just ignore the system type and always
synchronize your data properly.

-p

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Brown, Beverly
Sent: Thursday, March 30, 2006 5:52 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] DPC query

A DPC runs at a level where preemption is disabled on the processor
where it is running. One instance of a DPC is an interrupt DPC and would
be analogous to a Linux “bottom-half” interrupt (is that what they still
call it?). It can also be used to run a function to its entirety without
being preempted (I think Linux has a concept of FIFO scheduling - this
would be somewhat similar. The driver writer controls the queueing of a
DPC and they generally get run in FIFO order from the processor’s
queue.).

Blocking is not allowed in a DPC call. Data is protectected with a
spinlock on SMP systems in case something on the other processortries to
access the same data. On single processor systems, only device
interrupts (linux “top-half” interrupts) can preempt a DPC.

Beverly

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Vinay Kalkoti
Sent: Thursday, March 30, 2006 6:26 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] DPC query

Hi,

I come from Linux background and trying to understand Windows internals.

I am reading Inside Microsoft Windows 2000 by David Solomon.

I have a query about DPC. The book specified has something like this
about DPC.

"Dispatch or deferred procedure call (DPC) interrupts When a thread can
no longer continue executing, perhaps because it has terminated or
because it voluntarily enters a wait state, the kernel calls the
dispatcher directly to effect an immediate context switch. Sometimes,
however, the kernel detects that rescheduling should occur when it is
deep within many layers of code. In this situation, the ideal solution
is to request dispatching but defer its occurrence until the kernel
completes its current activity. Using a DPC software interrupt is a
convenient way to achieve this delay. "

It says that DPC’s defer the rescheduling of a process.

My questions are:

On what conditions we defer the rescheduling?

if we defer the rescheduling and if the current process blocks on any

object wont the CPU stay idle ?

Waiting for the response.

Thanks,
Vinay


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

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


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

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


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

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


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

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

Of course. I didn’t mean to imply that the drivers should be different
for UP vs MP. I was just offering a description of the behavior.

Beverly

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of David J. Craig
Sent: Thursday, March 30, 2006 12:42 PM
To: Windows System Software Devs Interest List
Subject: Re:[ntdev] DPC query

The use of different drivers for UP and MP probably wouldn’t work well
in any case. It might if they were dynamically loaded or based upon
wether the OS kernel is a UP or SMP edition. Otherwise someone could
have the /onecpu switch in the boot.ini on one boot option and not the
other. If it had the kernel & hal boot.ini switches you could have a
single system boot in UP and SMP mode as desired and have the UP and SMP
versions of the kernel too.
Don’t know why you would want it, but someone would try it.

“Peter Wieland” wrote in message
news:xxxxx@ntdev…
A few notes on beverly’s explaination:

There are two related concepts here - DPCs and interrupt level.
Interrupt level controls what can preempt the code currently running on
the processor and whether the thread can be rescheduled. Code running
at dispatch-level can still be preempted by a higher prioirty interrupt
(like a device interrupt) but will not get rescheduled.

You can think of a DPC as a way to inject a dispatch-level interrupt
into a processor. The DPC routine is sort of like a custom ISR. Since
the DPC routine runs at dispatch level it can still be pre-empted but
the thread it interrupted will not be rescheduled.

Also, unless you’re really ready to take on writing two drivers (one for
UP and one for MP), you should just ignore the system type and always
synchronize your data properly.

-p

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Brown, Beverly
Sent: Thursday, March 30, 2006 5:52 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] DPC query

A DPC runs at a level where preemption is disabled on the processor
where it is running. One instance of a DPC is an interrupt DPC and would
be analogous to a Linux “bottom-half” interrupt (is that what they still
call it?). It can also be used to run a function to its entirety without
being preempted (I think Linux has a concept of FIFO scheduling - this
would be somewhat similar. The driver writer controls the queueing of a
DPC and they generally get run in FIFO order from the processor’s
queue.).

Blocking is not allowed in a DPC call. Data is protectected with a
spinlock on SMP systems in case something on the other processortries to
access the same data. On single processor systems, only device
interrupts (linux “top-half” interrupts) can preempt a DPC.

Beverly

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Vinay Kalkoti
Sent: Thursday, March 30, 2006 6:26 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] DPC query

Hi,

I come from Linux background and trying to understand Windows internals.

I am reading Inside Microsoft Windows 2000 by David Solomon.

I have a query about DPC. The book specified has something like this
about DPC.

"Dispatch or deferred procedure call (DPC) interrupts When a thread can
no longer continue executing, perhaps because it has terminated or
because it voluntarily enters a wait state, the kernel calls the
dispatcher directly to effect an immediate context switch. Sometimes,
however, the kernel detects that rescheduling should occur when it is
deep within many layers of code. In this situation, the ideal solution
is to request dispatching but defer its occurrence until the kernel
completes its current activity. Using a DPC software interrupt is a
convenient way to achieve this delay. "

It says that DPC’s defer the rescheduling of a process.

My questions are:
# On what conditions we defer the rescheduling?
# if we defer the rescheduling and if the current process blocks on any
object wont the CPU stay idle ?

Waiting for the response.

Thanks,
Vinay


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

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


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

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


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

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

To clarify: Code running at dispatch level can still be INTERRUPTED by a higher priority interrupt (like a device interrupt). To avoid confusion, I would not use the term “preemption” to describe this activity (pre-emption being tied to the scheduling process).

I’m not trying to split hairs here, just trying to further clarify PeterWie’s already excellent description.

Peter
OSR

Folks,

Thanks for the respose. This is what I have understood from the mail
thread and from the book. Please correct me if I am wrong.

DPC/dispatch level is an IRQL which is basically used for
sychronizing objects. When we use spin locks for sychronizing certain
variable, the thread will be queued in the DPC queue if the lock is
held by other thread.

When the IRQL comes to DPC/dispatch level after the running thread
relingishes the IRQL, the kernel will check the DPC queue. If queue
is not empty, then the IRQL will be maintained at DPC/dispatch level
and the threads in the DPC queue will be dispatched.

Correct me if I am wrong.

Thanks,
Vinay

On 3/30/06, Brown, Beverly wrote:
> Of course. I didn’t mean to imply that the drivers should be different
> for UP vs MP. I was just offering a description of the behavior.
>
> Beverly
>
> -----Original Message-----
> From: xxxxx@lists.osr.com
> [mailto:xxxxx@lists.osr.com] On Behalf Of David J. Craig
> Sent: Thursday, March 30, 2006 12:42 PM
> To: Windows System Software Devs Interest List
> Subject: Re:[ntdev] DPC query
>
> The use of different drivers for UP and MP probably wouldn’t work well
> in any case. It might if they were dynamically loaded or based upon
> wether the OS kernel is a UP or SMP edition. Otherwise someone could
> have the /onecpu switch in the boot.ini on one boot option and not the
> other. If it had the kernel & hal boot.ini switches you could have a
> single system boot in UP and SMP mode as desired and have the UP and SMP
> versions of the kernel too.
> Don’t know why you would want it, but someone would try it.
>
> “Peter Wieland” wrote in message
> news:xxxxx@ntdev…
> A few notes on beverly’s explaination:
>
> There are two related concepts here - DPCs and interrupt level.
> Interrupt level controls what can preempt the code currently running on
> the processor and whether the thread can be rescheduled. Code running
> at dispatch-level can still be preempted by a higher prioirty interrupt
> (like a device interrupt) but will not get rescheduled.
>
> You can think of a DPC as a way to inject a dispatch-level interrupt
> into a processor. The DPC routine is sort of like a custom ISR. Since
> the DPC routine runs at dispatch level it can still be pre-empted but
> the thread it interrupted will not be rescheduled.
>
> Also, unless you’re really ready to take on writing two drivers (one for
> UP and one for MP), you should just ignore the system type and always
> synchronize your data properly.
>
> -p
>
> -----Original Message-----
> From: xxxxx@lists.osr.com
> [mailto:xxxxx@lists.osr.com] On Behalf Of Brown, Beverly
> Sent: Thursday, March 30, 2006 5:52 AM
> To: Windows System Software Devs Interest List
> Subject: RE: [ntdev] DPC query
>
> A DPC runs at a level where preemption is disabled on the processor
> where it is running. One instance of a DPC is an interrupt DPC and would
> be analogous to a Linux “bottom-half” interrupt (is that what they still
> call it?). It can also be used to run a function to its entirety without
> being preempted (I think Linux has a concept of FIFO scheduling - this
> would be somewhat similar. The driver writer controls the queueing of a
> DPC and they generally get run in FIFO order from the processor’s
> queue.).
>
> Blocking is not allowed in a DPC call. Data is protectected with a
> spinlock on SMP systems in case something on the other processortries to
> access the same data. On single processor systems, only device
> interrupts (linux “top-half” interrupts) can preempt a DPC.
>
> Beverly
>
> -----Original Message-----
> From: xxxxx@lists.osr.com
> [mailto:xxxxx@lists.osr.com] On Behalf Of Vinay Kalkoti
> Sent: Thursday, March 30, 2006 6:26 AM
> To: Windows System Software Devs Interest List
> Subject: [ntdev] DPC query
>
> Hi,
>
> I come from Linux background and trying to understand Windows internals.
>
> I am reading Inside Microsoft Windows 2000 by David Solomon.
>
> I have a query about DPC. The book specified has something like this
> about DPC.
>
> "Dispatch or deferred procedure call (DPC) interrupts When a thread can
> no longer continue executing, perhaps because it has terminated or
> because it voluntarily enters a wait state, the kernel calls the
> dispatcher directly to effect an immediate context switch. Sometimes,
> however, the kernel detects that rescheduling should occur when it is
> deep within many layers of code. In this situation, the ideal solution
> is to request dispatching but defer its occurrence until the kernel
> completes its current activity. Using a DPC software interrupt is a
> convenient way to achieve this delay. "
>
> It says that DPC’s defer the rescheduling of a process.
>
> My questions are:
> # On what conditions we defer the rescheduling?
> # if we defer the rescheduling and if the current process blocks on any
> object wont the CPU stay idle ?
>
> Waiting for the response.
>
> Thanks,
> Vinay
>
> —
> Questions? First check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer
>
>
> —
> Questions? First check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer
>
>
>
>
> —
> Questions? First check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer
>
>
> —
> Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256
>
> To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
>

Not really correct.

It’d be best if you read the chapter in our book (Windows NT Device Driver Development) on IRQLs and DPCs. But I s’pose if you HAD the book you wouldn’t be asking this question, right? So, here’s my briefest explanation. Do some googling, buy the book, read the DDK, and searching on OSR Online if you need a further explanation.

IRQLs are used by Windows as a mechanism for organizing and serializing certain activities. Each processor in the system can have a separate IRQL. The IRQL is necessary and sufficient to determine that procesor’s state with regard to pre-emptability, interruptability, and dispatchability.

Higher level IRQLs are modeled as interrupts. If the processor is running at lower IRQL when an interrupt is requested at a higher IRQL, the higher IRQL routine immediately executes. If the processor is running at a higher IRQL when a lower IRQL interrupt is requested, that lower IRQL routine is pended (waits) until the processor is about to return to an IRQL below that of the newly requested interrupt.

Normal thread execution takes place at IRQL PASSIVE_LEVEL.

Dispatching, thread scheduling, and a few other kernel mode activity run at IRQL DISPATCH_LEVEL.

Interrupt Service Routines for devices (ISRs) run at Device IRQLs, which are higher than IRQL DISPATCH_LEVEL.

Thread dispatching (scheduling) and thus pre-emption is blocked at IRQL DISPATCH_LEVEL and higher. Thus, whenever a processor is running at IRQL DISPATCH_LEVEL or higher the code is not pre-empted (by scheduling). Of course, ANY code running on the system can be INTERRUPTED by a higher IRQL interrupt. So, for example, code running at IRQL DISPATCH_LEVEL could be interrupted by a device interrupt service routine (which runs at a Device IRQL).

IRQL DISPATCH_LEVEL has numerous special uses. Deferred Procedure Calls (DPCs) run at IRQL DISPATCH_LEVEL. DPCs are kernel-mode callbacks that queued by device drivers and the operating system. These callbacks are invoked whenever the operating system is otherwise ready to return to an IRQL less than DISPATCH_LEVEL, such as when exiting the operating system and returning to run a user-mode thread.

Because code running at IRQL DISPATCH_LEVEL is run to completion, this IRQL is used in the implementation of Windows executive spin locks (these are just one specific type of spin lock on Windows… there are others). When an executive spin lock is acquired on a multiprocessor system, two things happen: (a) The IRQL of the executing processor is raised to IRQL DISPATCH_LEVEL and (b) The spinlock is acquired with an interlocked instruction. Raising the IRQL on the current processor ensures that the current thread will not be rescheduled… thus ensuring serialized access (at IRQL DISPATCH_LEVEL and below) to any shared data ON THAT CPU. Acquiring the spinlock ensures that code executing on other processors on the system do not simultaneously access the protected data.

That’s a pretty dense explanation, and it ignores a few details (IRQL APC_LEVEL, how the DPC list is maintained and drained, etc). But that should give you the understanding you need while you search out more details.

Peter
OSR

No that’s not right - you’ve confused several elements.

We’re talking about four different things here: DPCs, IRQL (including
DISPATCH_LEVEL), threads, and interrupts.

A DPC is a “deferred proceedure call” that you can enqueue to run on a
processor that will interrupt the processor when it’s IRQL is below
DISPATCH_LEVEL, but it is not the same thing as DISPATCH_LEVEL. DPCs
are NOT threads.

IRQL is the interrupt level at which the processor is running.
PASSIVE_LEVEL is the lowest level and is where processors spend most of
their life, running code from various threads of execution.
DISPATCH_LEVEL is a higher interrupt level, and a processor running at
DISPATCH_LEVEL does no scheduling. A processor can reach DISPATCH_LEVEL
in one of two ways - the running thread can be interrupted with a DPC,
or the running thread can call KeRaiseIrql to change the IRQL.

A thread is a persistent unit of execution. A thread can be suspended,
moved to another processor, or rescheduled if another higher priority
thread needs to run. The kernel decides what thread should be running
on each processor at any given time. That thread will run until it
deliberately yields the processor (say by waiting on a dispatcher
object), or until a timer interrupt decides that something else should
run.

Which brings us nicely to interrupts. Threads are one way a processor
is directed to run code. Interrupts are another (perhaps the other).
There are two types of interrupts. The first are hardware interrupts,
the interrupts you’re used to that come from devices. The second type
are software interrupts, interrupts sent by the kernel or HAL in order
to interrupt a processor and direct it to run some other code for a
little while - something like a DPC routine.

Let’s look at some examples:

Processor A has thread T running on it. Procesor B enqueues a DPC which
the kernel decides to run on processor A. This causes a software
interrupt to be sent to processor A

If A is at PASSIVE_LEVEL then the running thread is interrupted. The
kernel raises the IRQL to DISPATCH_LEVEL, then runs all the DPCs in the
DPC queue. When it’s done it attempts to restore the IRQL to
PASSIVE_LEVEL and lets the thread continue running.

But let’s say T calls KeRaiseIrql(DISPATCH_LEVEL) before the interrupt
arrives. The incoming interrupt is not a higher interrupt level, so the
processor is not interrupted. T continues to run until it calls
KeLowerIrql(…) to go back to PASSIVE_LEVEL. At this point, where the
IRQL drops below DISPATCH_LEVEL, the processor is interrupted. The
interrupt handler will run through and run any queued DPCs before
returning. When it returns the processor is allowed to return to
PASSIVE_LEVEL and the thread T regains control.

Now say thread T wants to acquire a spinlock. It raises IRQL to
DISPATCH_LEVEL and then spins waiting for the lock to be available*.
Spinlock acquisition always happens at or above DISPATCH_LEVEL. If
processor B already owns the lock, then the T will simply spin at
DISPATCH_LEVEL until B releases the lock**. The DPC queue is never
involved in this case.

-p

* - the raise and the spin are both done by KeAcquireSpinLock for you.

** - as an exercise for the reader, think about how I know that
processor A cannot already own the lock if the thread running on
processor A is trying to acquire it (ignoring a broken program)

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Vinay Kalkoti
Sent: Thursday, March 30, 2006 10:49 PM
To: Windows System Software Devs Interest List
Subject: Re: [ntdev] DPC query

Folks,

Thanks for the respose. This is what I have understood from the mail
thread and from the book. Please correct me if I am wrong.

DPC/dispatch level is an IRQL which is basically used for sychronizing
objects. When we use spin locks for sychronizing certain variable, the
thread will be queued in the DPC queue if the lock is held by other
thread.

When the IRQL comes to DPC/dispatch level after the running thread
relingishes the IRQL, the kernel will check the DPC queue. If queue is
not empty, then the IRQL will be maintained at DPC/dispatch level and
the threads in the DPC queue will be dispatched.

Correct me if I am wrong.

Thanks,
Vinay

On 3/30/06, Brown, Beverly wrote:
> Of course. I didn’t mean to imply that the drivers should be different

> for UP vs MP. I was just offering a description of the behavior.
>
> Beverly
>
> -----Original Message-----
> From: xxxxx@lists.osr.com
> [mailto:xxxxx@lists.osr.com] On Behalf Of David J. Craig
> Sent: Thursday, March 30, 2006 12:42 PM
> To: Windows System Software Devs Interest List
> Subject: Re:[ntdev] DPC query
>
> The use of different drivers for UP and MP probably wouldn’t work
> well in any case. It might if they were dynamically loaded or based
> upon wether the OS kernel is a UP or SMP edition. Otherwise someone
> could have the /onecpu switch in the boot.ini on one boot option and
> not the other. If it had the kernel & hal boot.ini switches you could

> have a single system boot in UP and SMP mode as desired and have the
> UP and SMP versions of the kernel too.
> Don’t know why you would want it, but someone would try it.
>
> “Peter Wieland” wrote in message
> news:xxxxx@ntdev…
> A few notes on beverly’s explaination:
>
> There are two related concepts here - DPCs and interrupt level.
> Interrupt level controls what can preempt the code currently running
> on the processor and whether the thread can be rescheduled. Code
> running at dispatch-level can still be preempted by a higher prioirty
> interrupt (like a device interrupt) but will not get rescheduled.
>
> You can think of a DPC as a way to inject a dispatch-level interrupt
> into a processor. The DPC routine is sort of like a custom ISR.
> Since the DPC routine runs at dispatch level it can still be
> pre-empted but the thread it interrupted will not be rescheduled.
>
> Also, unless you’re really ready to take on writing two drivers (one
> for UP and one for MP), you should just ignore the system type and
> always synchronize your data properly.
>
> -p
>
> -----Original Message-----
> From: xxxxx@lists.osr.com
> [mailto:xxxxx@lists.osr.com] On Behalf Of Brown, Beverly
> Sent: Thursday, March 30, 2006 5:52 AM
> To: Windows System Software Devs Interest List
> Subject: RE: [ntdev] DPC query
>
> A DPC runs at a level where preemption is disabled on the processor
> where it is running. One instance of a DPC is an interrupt DPC and
> would be analogous to a Linux “bottom-half” interrupt (is that what
> they still call it?). It can also be used to run a function to its
> entirety without being preempted (I think Linux has a concept of FIFO
> scheduling - this would be somewhat similar. The driver writer
> controls the queueing of a DPC and they generally get run in FIFO
> order from the processor’s queue.).
>
> Blocking is not allowed in a DPC call. Data is protectected with a
> spinlock on SMP systems in case something on the other processortries
> to access the same data. On single processor systems, only device
> interrupts (linux “top-half” interrupts) can preempt a DPC.
>
> Beverly
>
> -----Original Message-----
> From: xxxxx@lists.osr.com
> [mailto:xxxxx@lists.osr.com] On Behalf Of Vinay Kalkoti
> Sent: Thursday, March 30, 2006 6:26 AM
> To: Windows System Software Devs Interest List
> Subject: [ntdev] DPC query
>
> Hi,
>
> I come from Linux background and trying to understand Windows
internals.
>
> I am reading Inside Microsoft Windows 2000 by David Solomon.
>
> I have a query about DPC. The book specified has something like this
> about DPC.
>
> "Dispatch or deferred procedure call (DPC) interrupts When a thread
> can no longer continue executing, perhaps because it has terminated or

> because it voluntarily enters a wait state, the kernel calls the
> dispatcher directly to effect an immediate context switch. Sometimes,
> however, the kernel detects that rescheduling should occur when it is
> deep within many layers of code. In this situation, the ideal solution

> is to request dispatching but defer its occurrence until the kernel
> completes its current activity. Using a DPC software interrupt is a
> convenient way to achieve this delay. "
>
> It says that DPC’s defer the rescheduling of a process.
>
> My questions are:
> # On what conditions we defer the rescheduling?
> # if we defer the rescheduling and if the current process blocks on
> any object wont the CPU stay idle ?
>
> Waiting for the response.
>
> Thanks,
> Vinay
>
> —
> Questions? First check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer
>
>
> —
> Questions? First check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer
>
>
>
>
> —
> Questions? First check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer
>
>
> —
> Questions? First check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer
>


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

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

>I have a query about DPC.

Nearly the same as “bottom half” in Linux. KeInsertQueueDpc is similar to
mark_bh().

It says that DPC’s defer the rescheduling of a process.

They are running in a more raised context then threads.

My questions are:

On what conditions we defer the rescheduling?

The DPC queue is drained (all queued DPCs are called, this occurs in the
KiRetireDpcList kernel function) on lowering the IRQL from >= DISPATCH to <
DISPATCH, and not on all these lowerings.

For instance, the DPC queue is always drained on the timer IRQ epilog, it is
also always drained if the ISR (in which’s epilog the IRQL lower is) has
interrupted the idle CPU, and in some other cases governed by IdealDpcRate and
other similar registry settings. There is also per-DPC importance value, which
also influences this (IIRC the High importance DPCs are always executed on any
IRQL lowering from >= DISPATCH to < DISPATCH).

No thread is executed during this DPC draining. The CPU returns to thread
execution only after this.

In fact, the DPCs are more low-level notion then a scheduler itself.

if we defer the rescheduling and if the current process blocks on

any object wont the CPU stay idle ?

No. DPCs are delivered regardless of what is the current process, and what it
does. BTW - if the thread blocks on any object, it ceases to be the “current
thread” immediately. So, the current thread cannot block :slight_smile:

Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com

>DPC/dispatch level is an IRQL which is basically used for

sychronizing objects.

It is the IRQL on which the scheduler is temporary blocked. It is the IRQL on
which the scheduler code itself is running. It is the IRQL which is raised to
on any spinlock acquisition.

You cannot wait on any object on this IRQL. This is conceptually similar to
“nonblocking” code in Linux - remember, in Linux, the code holding a spinlock
cannot wait on any object? so is in Windows.

More so. VM in-page operation requires to wait for inpage to complete. That is
why you cannot touch anything pageable - be it code or data - on >=
DISPATCH_LEVEL.

When we use spin locks for sychronizing certain
variable, the thread will be queued in the DPC queue if the lock is
held by other thread.

No. Spinlocks are very low-level and not connected with the scheduler at all.
Spinlock is just a spin on a single LOCK BTS opcode. They do not know on
threads.

When the IRQL comes to DPC/dispatch level after the running thread
relingishes the IRQL, the kernel will check the DPC queue.

No. When the attempt is made to drop the IRQL below DISPATCH.

is not empty, then the IRQL will be maintained at DPC/dispatch level
and the threads in the DPC queue will be dispatched.

This occurs not on each such event, there are some more limitations on when the
DPC queue is drained, but the general concept is such.

Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com

On 3/31/06, Peter Wieland wrote:
> No that’s not right - you’ve confused several elements.
>
> We’re talking about four different things here: DPCs, IRQL (including
> DISPATCH_LEVEL), threads, and interrupts.
>
> A DPC is a “deferred proceedure call” that you can enqueue to run on a
> processor that will interrupt the processor when it’s IRQL is below
> DISPATCH_LEVEL, but it is not the same thing as DISPATCH_LEVEL. DPCs
> are NOT threads.
>
> IRQL is the interrupt level at which the processor is running.
> PASSIVE_LEVEL is the lowest level and is where processors spend most of
> their life, running code from various threads of execution.
> DISPATCH_LEVEL is a higher interrupt level, and a processor running at
> DISPATCH_LEVEL does no scheduling. A processor can reach DISPATCH_LEVEL
> in one of two ways - the running thread can be interrupted with a DPC,
> or the running thread can call KeRaiseIrql to change the IRQL.
>
> A thread is a persistent unit of execution. A thread can be suspended,
> moved to another processor, or rescheduled if another higher priority
> thread needs to run. The kernel decides what thread should be running
> on each processor at any given time. That thread will run until it
> deliberately yields the processor (say by waiting on a dispatcher
> object), or until a timer interrupt decides that something else should
> run.
>
> Which brings us nicely to interrupts. Threads are one way a processor
> is directed to run code. Interrupts are another (perhaps the other).
> There are two types of interrupts. The first are hardware interrupts,
> the interrupts you’re used to that come from devices. The second type
> are software interrupts, interrupts sent by the kernel or HAL in order
> to interrupt a processor and direct it to run some other code for a
> little while - something like a DPC routine.
>
> Let’s look at some examples:
>
> Processor A has thread T running on it. Procesor B enqueues a DPC which
> the kernel decides to run on processor A. This causes a software
> interrupt to be sent to processor A
>
> If A is at PASSIVE_LEVEL then the running thread is interrupted. The
> kernel raises the IRQL to DISPATCH_LEVEL, then runs all the DPCs in the
> DPC queue. When it’s done it attempts to restore the IRQL to
> PASSIVE_LEVEL and lets the thread continue running.

[Vinay]: So, the DPC runs in the context of the thread T on processor
A. Right ???
>
> But let’s say T calls KeRaiseIrql(DISPATCH_LEVEL) before the interrupt
> arrives. The incoming interrupt is not a higher interrupt level, so the
> processor is not interrupted. T continues to run until it calls
> KeLowerIrql(…) to go back to PASSIVE_LEVEL. At this point, where the
> IRQL drops below DISPATCH_LEVEL, the processor is interrupted. The
> interrupt handler will run through and run any queued DPCs before
> returning. When it returns the processor is allowed to return to
> PASSIVE_LEVEL and the thread T regains control.
>
>
> Now say thread T wants to acquire a spinlock. It raises IRQL to
> DISPATCH_LEVEL and then spins waiting for the lock to be available*.
> Spinlock acquisition always happens at or above DISPATCH_LEVEL. If
> processor B already owns the lock, then the T will simply spin at
> DISPATCH_LEVEL until B releases the lock . The DPC queue is never
> involved in this case.
>
> -p
>
> * - the raise and the spin are both done by KeAcquireSpinLock for you.
>
>
- as an exercise for the reader, think about how I know that
> processor A cannot already own the lock if the thread running on
> processor A is trying to acquire it (ignoring a broken program)
>