16.2.1  Providing Storage for Spin Locks and Protected Data

When an NT driver is loaded, its DriverEntry routine must allocate resident storage for any spin-lock-protected data or resources and for corresponding spin locks in one of the following places:

·The device extension of a device object that the driver sets up by calling IoCreateDevice

·The controller extension of a controller object that the driver sets up by calling IoCreateController

·Nonpaged, system-space memory that the driver obtains by calling ExAllocatePool

Attempting to access pageable data while holding a spin lock causes a fatal page fault if that page is not present. Referencing a spin lock that is invalid (because it was stored in pageable memory and is currently paged-out) also causes a fatal page fault.

An NT driver must provide the storage for each of the following kinds of executive spin lock it might use:

·Any spin lock used to synchronize access to driver-defined data by nonISR driver routines that call KeAcquireSpinLock and KeReleaseSpinLock or KeAcquireSpinLockAtDpcLevel and KeReleaseSpinLockFromDpcLevel

·Any spin lock used to synchronize access to a driver-allocated resource by calling a resource-specific set of ExInterlockedXxx routines

While a driver can make calls to the ExInterlocked..List and/or ExInterlocked..Zone routines from its ISR or SynchCritSection routines, it cannot call KeAcquireSpinLock and KeReleaseSpinLock or KeAcquireSpinLockAtDpcLevel and KeReleaseSpinLockFromDpcLevel while running at IRQL greater than DISPATCH_LEVEL. Consequently, any driver that reuses a spin lock in calls to the Ke..SpinLock and ExInterlockedXxx routines must make every call while running at IRQL <= DISPATCH_LEVEL.

Note that a driver can pass the same spin lock to ExInterlockedInsertHeadList as it does to ExInterlockedAllocateFromZone. For more information about how spin lock usage affects performance, see Section 16.2.4. For more information about using zone buffers, see Section 16.4.3.

In addition to the storage for its executive spin locks, an NT device driver must provide the storage for another spin lock to be associated with its interrupt object(s) if it has a multivector ISR or more than one ISR.