locked
DPC processing RRS feed

  • Question

  • DPC queue processing happens before the dispatcher selects a thread and assigns the CPU to it. 

    vs 

    If a DPC queue has one Thread scheduler and 2 DpcforIsr, How does windows execute these DPC routines?

    Answer to the above question :

    The thread scheduler DPC will CHOOSE which normal thread to run next.  The thread it chooses will not actually run until all of the DPCs are finished and the IRQL drops below DISPATCH_LEVEL.

    My question:

    Which DPC runs first? I assume the thread scheduler DPC as the DPC queue follows FIFO order. Am i correct?

    • Edited by Boomi.s Wednesday, October 21, 2015 5:12 PM
    Wednesday, October 21, 2015 4:12 AM

Answers

  • Insertion is not any order; it is either to the back or the front of the queue. Yes, removal is always from the front. But, because CPUs could steal DPCs from each other, and then get interrupted, there is no ordering guarantee for the order of the DPCs being executed, so do not depend upon it (i.e. if you submit DPC A, followed by DPC B, there is no guarantee that A will be called before B). For the same reason, there is no guarantee that setting the target processor ID in a DPC will cause that DPC to run on the requested CPU.

     -Brian


    Azius Developer Training www.azius.com Windows device driver, internals, security, & forensics training and consulting. Blog at www.azius.com/blog

    • Marked as answer by Boomi.s Wednesday, October 21, 2015 10:57 PM
    Wednesday, October 21, 2015 10:14 PM
  • DPC processing has become rather complex. First, there are two DPC queues per CPU, one for normal DPCs and another for threaded DPCs. Forget any notion of strict FIFO or LIFO. Normal DPCs are processed before threaded DPCs. The DPC dispatcher (KiExecuteAllDpcs, which is called by KiRetireDpcList) always pulls DPC work items from the head of the queue, but items are not always added to the queue in FIFO, based upon whether or not the importance is set to HighImportance or not (non-HighImportance entries to go to the tail of the queue). Also, DPCs can be targeted at a particular CPU, causing the DPC to be placed on that CPU's DPC queue. Note, that CPUs that become idle will steal DPCs from the DPC queues of non-idle CPUs.

     -Brian


    Azius Developer Training www.azius.com Windows device driver, internals, security, & forensics training and consulting. Blog at www.azius.com/blog

    • Marked as answer by Brian Catlin Wednesday, October 21, 2015 9:20 PM
    Wednesday, October 21, 2015 9:20 PM

All replies

  • It is mostly FIFO, but check out KeSetImportanceDpc https://msdn.microsoft.com/en-us/library/windows/hardware/ff553259%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396  Since this does allow overriding the model, and placing the DPC such that it is LIFO.


    Don Burn Windows Driver Consulting Website: http://www.windrvr.com

    Wednesday, October 21, 2015 7:35 PM
  • DPC processing has become rather complex. First, there are two DPC queues per CPU, one for normal DPCs and another for threaded DPCs. Forget any notion of strict FIFO or LIFO. Normal DPCs are processed before threaded DPCs. The DPC dispatcher (KiExecuteAllDpcs, which is called by KiRetireDpcList) always pulls DPC work items from the head of the queue, but items are not always added to the queue in FIFO, based upon whether or not the importance is set to HighImportance or not (non-HighImportance entries to go to the tail of the queue). Also, DPCs can be targeted at a particular CPU, causing the DPC to be placed on that CPU's DPC queue. Note, that CPUs that become idle will steal DPCs from the DPC queues of non-idle CPUs.

     -Brian


    Azius Developer Training www.azius.com Windows device driver, internals, security, & forensics training and consulting. Blog at www.azius.com/blog

    • Marked as answer by Brian Catlin Wednesday, October 21, 2015 9:20 PM
    Wednesday, October 21, 2015 9:20 PM
  • From the answers, my understanding is:

    Inserting DPC object to DPC queue can be in any order based on the importance of the DPC. But processing of the DPCs from the queue is always in FIFO order. As the items are pulled from the head of the queue. 

    Is that correct?

    Wednesday, October 21, 2015 9:46 PM
  • Insertion is not any order; it is either to the back or the front of the queue. Yes, removal is always from the front. But, because CPUs could steal DPCs from each other, and then get interrupted, there is no ordering guarantee for the order of the DPCs being executed, so do not depend upon it (i.e. if you submit DPC A, followed by DPC B, there is no guarantee that A will be called before B). For the same reason, there is no guarantee that setting the target processor ID in a DPC will cause that DPC to run on the requested CPU.

     -Brian


    Azius Developer Training www.azius.com Windows device driver, internals, security, & forensics training and consulting. Blog at www.azius.com/blog

    • Marked as answer by Boomi.s Wednesday, October 21, 2015 10:57 PM
    Wednesday, October 21, 2015 10:14 PM
  • One more question. 

    When DPC inserted in the DPC Queue, DISPATCH_LEVEL software interrupt is typically generated on the processor. Which function dose this? Is it available to the driver developer?

    Thursday, October 22, 2015 3:22 PM
  • Whether a dispatcher interrupt is requested by KeInsertQueueDpc depends upon the DPC's priority, the number of entries in the DPC queue, whether the DPC is targeted at another CPU, and the DPC request rate. Some of these counters can be seen in PerfMon.

    HighImportance or MediumHighImportance will trigger an interrupt. The difference is that HighImportance DPCs are placed at the head of the queue, and MediumHighImportance DPCs are placed at the tail of the queue. The default, MediumImportance DPCs are placed at the tail of the queue and an interrupt is requested only if the DPC queue depth is too high, or the DPC rate is too low. LowImportance DPCs are placed at the tail of the queue, and only generate an interrupt if the DPC rate is too low.

    No, you cannot request a dispatcher interrupt explicitly from a driver.

     -Brian


    Azius Developer Training www.azius.com Windows device driver, internals, security, & forensics training and consulting. Blog at www.azius.com/blog

    Thursday, October 22, 2015 5:55 PM