Driver Problems? Questions? Issues?
Put OSR's experience to work for you! Contact us for assistance with:
  • Creating the right design for your requirements
  • Reviewing your existing driver code
  • Analyzing driver reliability/performance issues
  • Custom training mixed with consulting and focused directly on your specific areas of interest/concern.
Check us out. OSR, the Windows driver experts.

OSR Seminars


Go Back   OSR Online Lists > ntdev
Welcome, Guest
You must login to post to this list
  Message 1 of 11  
12 Feb 18 01:00
Michael Rolle
xxxxxx@rolle.name
Join Date: 27 Dec 2017
Posts To This List: 59
Can I have a KDPC object on the stack? When can I destroy or alter a KDPC?

AMD's CodeXL driver implements DPCs by declaring a local KDPC variable, initializing it, queueing it, creating an event, and waiting for the deferred routine to post to the event. The data passed to the deferred routine is also in a local variable. I assume this is safe, because AMD's product is out there. But it doesn't follow the documented requirement that it be in a device/controller extension or somewhere allocated from a nonpaged pool. Is the kernel stack in nonpaged memory, too? I made the mistake recently of creating my KDPC on the stack, but not waiting for the deferred routine to finish, so that the KDPC would have quickly been overwritten. This resulted in a bugcheck. So I'm putting my KDPC in the device extension. But that raises the question of reusing the KDPC, and synchronizing usage between threads. Assuming that I've handled the issue of access by different threads, I need to know how long a thread that is using the KDPC needs hold onto it, after queueing it. Even when the device is only being used by one user File, there may be multiple IRPs coming from different user threads at the same time.
  Message 2 of 11  
12 Feb 18 01:36
Tim Roberts
xxxxxx@probo.com
Join Date: 28 Jan 2005
Posts To This List: 11922
Can I have a KDPC object on the stack? When can I destroy or alter a KDPC?

On Feb 11, 2018, at 10:00 PM, xxxxx@rolle.name <xxxxx@lists.osr.com> wrote: > > AMD's CodeXL driver implements DPCs by declaring a local KDPC variable, initializing it, queueing it, creating an event, and waiting for the deferred routine to post to the event. The data passed to the deferred routine is also in a local variable. > > I assume this is safe, because AMD's product is out there. But it doesn't follow the documented requirement that it be in a device/controller extension or somewhere allocated from a nonpaged pool. > > Is the kernel stack in nonpaged memory, too? As long as the thread is running, or in a "kernel mode" wait, the stack is locked into memory. The kernel stack can only be paged out if you do a wait that specifies "user mode". ??? Tim Roberts, xxxxx@probo.com Providenza & Boekelheide, Inc.
  Message 3 of 11  
17 Feb 18 13:09
matt sykes
xxxxxx@hotmail.com
Join Date:
Posts To This List: 221
Can I have a KDPC object on the stack? When can I destroy or alter a KDPC?

If the KDPC is on the stack it gets destroyed when the function exits. Trying to access it after wards is a recipe for disaster. Just wait for the DPC routine to finish.
  Message 4 of 11  
20 Feb 18 12:19
Peter Viscarola (OSR)
xxxxxx@osr.com
Join Date:
Posts To This List: 6146
List Moderator
Can I have a KDPC object on the stack? When can I destroy or alter a KDPC?

<quote> Just wait for the DPC routine to finish. </quote> Just don't put the DPC object on the stack! Kinda of a dumb practice. Peter OSR @OSRDrivers
  Message 5 of 11  
21 Feb 18 10:10
matt sykes
xxxxxx@hotmail.com
Join Date:
Posts To This List: 221
Can I have a KDPC object on the stack? When can I destroy or alter a KDPC?

Why tell me Peter? I am quite aware of that. Why not tell the OP?
  Message 6 of 11  
21 Feb 18 17:01
Peter Viscarola (OSR)
xxxxxx@osr.com
Join Date:
Posts To This List: 6146
List Moderator
Can I have a KDPC object on the stack? When can I destroy or alter a KDPC?

>Why tell me Peter? I wasn't speaking directly to you, Mr. Sykes. I was simply adding a "better alternative" to waiting for the DPC routine to finish. Turns out, ANYbody can read the threads on this forum. Peter OSR @OSRDrivers
  Message 7 of 11  
22 Feb 18 04:11
matt sykes
xxxxxx@hotmail.com
Join Date:
Posts To This List: 221
Can I have a KDPC object on the stack? When can I destroy or alter a KDPC?

Perhaps best not to quote the person you are not replying to. :)
  Message 8 of 11  
22 Feb 18 11:50
Peter Viscarola (OSR)
xxxxxx@osr.com
Join Date:
Posts To This List: 6146
List Moderator
Can I have a KDPC object on the stack? When can I destroy or alter a KDPC?

>Perhaps best not to quote the person you are not replying to. :) Perhaps best: 1) Not to be so quick to take offense 2) Expand your horizons in terms of forum protocol. I was quoting what you said, not specifically replying to you, Mr. Sykes. If I had SPECIFICALLY intended to address you, I would have used your name, or prefixed my reply with an @ I quoted you because I did not think this PARTICULAR piece of advice that you gave was the BEST advice the OP, or somebody else reading the list later on, could receive on this topic. It was "fine" and correct as far as it went... but it was not the fix of the root cause of the OP's issue... which was for him to not do what he was doing, because what he was doing was not best practice. On this list, a quote is not a "reply to" a specific person unless that person is named. It is a reply to the THREAD in question. 3) Not to tell the list owner how to run the list -- IIRC, you and I have butted heads before, Mr. Sykes. Please keep in mind that when you post to this list, you are a guest in my house. This is true for anybody who posts here, of course. I trust I do not need to explain this precept further, eh? Peter OSR @OSRDrivers
  Message 9 of 11  
26 Feb 18 06:52
matt sykes
xxxxxx@hotmail.com
Join Date:
Posts To This List: 221
Can I have a KDPC object on the stack? When can I destroy or alter a KDPC?

Perhaps in his situation this is the best way to allocate a KDPC, we don't know, but there could be a good reason for it, hence my answering his basic question. Of course we can then go on to ask him what he is trying to do, and suggest a better design, but we hadn't got to that stage yet. And I dont think we have butted heads Peter. Dont forget, it is easy to misinterpret a persons sentiment in written text, I was not taking offence, so don't worry about it. :)
  Message 10 of 11  
03 Mar 18 18:39
Michael Rolle
xxxxxx@rolle.name
Join Date: 27 Dec 2017
Posts To This List: 59
Can I have a KDPC object on the stack? When can I destroy or alter a KDPC?

From the OP himself... I have my DPC stored within my device extension, so I don't have any issue with it getting blown off the stack. However, I do still have the question, which hasn't been answered: How soon after KeInsertQueueDpc can I reuse the DPC? My guess is that the DPC is "owned" by the system, and should remain unchanged, only as long as it is still on the queue; and when the DPC routine starts to run, that means the DPC has been removed from the queue. Indeed, the DPC routine itself should be able queue the DPC again.
  Message 11 of 11  
03 Mar 18 20:58
Tim Roberts
xxxxxx@probo.com
Join Date: 28 Jan 2005
Posts To This List: 11922
Can I have a KDPC object on the stack? When can I destroy or alter a KDPC?

On Mar 3, 2018, at 3:39 PM, xxxxx@rolle.name <xxxxx@lists.osr.com> wrote: > > However, I do still have the question, which hasn't been answered: How soon after KeInsertQueueDpc can I reuse the DPC? > > My guess is that the DPC is "owned" by the system, and should remain unchanged, only as long as it is still on the queue; and when the DPC routine starts to run, that means the DPC has been removed from the queue. Indeed, the DPC routine itself should be able queue the DPC again. What do you mean by "reuse"? Once you have initialized a DPC, you can call KeInsertQueueDpc as often as you want. There are only two possible conditions: 1. The DPC is not queued. In that case, the DPC will be queued and KeInsertQueueDPC returns true. 2. The DPC is queued, but is not running. In that case, KeInsertQueueDPC does nothing and returns false. If the DPC function is running, then the DPC has been dequeued, and condition 1 applies. If you mean "when can I call KeInitializeDpc again", the answer is "any time the DPC is not queued". There is no documented way to find out whether it is currently queued or not, mostly because that state can change at any time. You would need to track that. ??? Tim Roberts, xxxxx@probo.com Providenza & Boekelheide, Inc.
Posting Rules  
You may not post new threads
You may not post replies
You may not post attachments
You must login to OSR Online AND be a member of the ntdev list to be able to post.

All times are GMT -5. The time now is 15:26.


Copyright ©2015, OSR Open Systems Resources, Inc.
Based on vBulletin Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Modified under license