[WDM] When should I acquire and release RemoveLock?

Hi everyone

Looking at drivers code, I found myself confused about when I should acquire/release a RemoveLock.
I know this must be related to PNP and Power management, but I haven’t found any useful resource that spill information about that.

I see some drivers provide PNP and POWER dispatch routines and do acquire the RemoveLock during operations which the driver cannot be removed, but I see some other drivers that doesn’t implement those and don’t need to worry about locking

Can someone help me understand this?

Thanks.

The docs are pretty clear, I think: https://docs.microsoft.com/en-us/windows-hardware/drivers/kernel/using-remove-locks

The one thing that’s not necessarily clear in the docs is that Remove Locks Remove locks aren’t an architectural part of Windows… they’re just a tool that (works entirely within your driver and) was created to make it easier for WDM drivers to track when their device can be destructed. This was long before KMDF was created.

So, some drivers use them… some don’t. I’ve never used a Remove Lock in a WDM driver… probably because I started writing WDM drivers before there were Remove Locks. I simply used my own code that accomplishes the same goal.

So, you need to track when your Device Object can be destructed somehow, but you don’t have to use a Remove Lock to accomplish this.

Peter