Here is !locks output along with all threads stacks referenced in it.
1: kd> !locks
**** DUMP OF ALL RESOURCE OBJECTS ****
Resource @ 0xffffe001d30e5010 Exclusively owned
Contention Count = 1
NumberOfExclusiveWaiters = 1
Threads: ffffe001d61d9080-01<*>
Threads Waiting On Exclusive Access:
ffffe001d4917880
Resource @ 0xffffe001d640a9a0 Exclusively owned
Contention Count = 30
NumberOfExclusiveWaiters = 1
Threads: ffffe001d61d9080-01<*>
Threads Waiting On Exclusive Access:
ffffe001d2fcc880
Resource @ 0xffffe001d30e65e0 Shared 2 owning threads
Threads: ffffe001d4b71040-01<*> ffffe001d61d9080-01<*>
Resource @ 0xffffe001d30e6560 Exclusively owned
Threads: ffffe001d61d9080-01<*>
KD: Scanning for held locks.
9884 total locks, 4 locks currently held
1: kd> !thread ffffe001d61d9080; !thread ffffe001d4917880; !thread ffffe001d61d9080; !thread ffffe001d2fcc880; !thread ffffe001d4b71040; !thread ffffe001d61d9080; !thread ffffe001d61d9080
THREAD ffffe001d61d9080 Cid 05a8.03cc Teb: 00007ff6d6cf0000 Win32Thread: fffff901406b3010 WAIT: (WrVirtualMemory) KernelMode Non-Alertable
ffffd00020b73200 Gate
IRP List:
ffffe001d42db530: (0006,0670) Flags: 00000404 Mdl: 00000000
Not impersonating
DeviceMap ffffc001171766d0
Owning Process ffffe001d5a35840 Image: explorer.exe
Attached Process N/A Image: N/A
Wait Start TickCount 54993 Ticks: 2395 (0:00:00:37.421)
Context Switch Count 968 IdealProcessor: 1
UserTime 00:00:00.000
KernelTime 00:00:02.265
Win32 Start Address 0x00007fff25301df0
Stack Init ffffd00020b73c90 Current ffffd00020b72ee0
Base ffffd00020b74000 Limit ffffd00020b6e000 Call 0
Priority 15 BasePriority 8 UnusualBoost 5 ForegroundBoost 2 IoPriority 2 PagePriority 2
Call Site
nt!KiSwapContext+0x76
nt!KiSwapThread+0x14e
nt!KiCommitThreadWait+0x129
nt!KeWaitForGate+0x10b
nt!MiWaitForPageWriteCompletion+0xb5
nt!MiFlushSectionInternal+0xb48
nt!MmFlushSection+0x1a2
nt!CcFlushCachePriv+0x493
nt!CcFlushCache+0x11
MYDRIVER!PreCleanup+0x2bd
fltmgr!FltpPerformPreCallbacks+0x29f
fltmgr!FltpPassThroughInternal+0x8c
fltmgr!FltpPassThrough+0x2be
fltmgr!FltpDispatch+0x9a
nt!IopCloseFile+0x12d
nt!NtClose+0x1c3
nt!KiSystemServiceCopyEnd+0x13 (TrapFrame @ ffffd00020b73b00) 0x00007fff
29c60d3a
THREAD ffffe001d4917880 Cid 05a8.0ba4 Teb: 00007ff6d6d2c000 Win32Thread: fffff90142213b60 WAIT: (WrResource) KernelMode Non-Alertable
ffffe001d44c5be0 SynchronizationEvent
IRP List:
ffffe001d4278990: (0006,0670) Flags: 00000884 Mdl: 00000000
Not impersonating
DeviceMap ffffc001171766d0
Owning Process ffffe001d5a35840 Image: explorer.exe
Attached Process N/A Image: N/A
Wait Start TickCount 57348 Ticks: 40 (0:00:00:00.625)
Context Switch Count 838 IdealProcessor: 0
UserTime 00:00:00.000
KernelTime 00:00:00.312
Win32 Start Address 0x00007fff29c033a0
Stack Init ffffd0002351bc90 Current ffffd0002351a5c0
Base ffffd0002351c000 Limit ffffd00023516000 Call 0
Priority 11 BasePriority 8 UnusualBoost 0 ForegroundBoost 2 IoPriority 2 PagePriority 5
Call Site
nt!KiSwapContext+0x76
nt!KiSwapThread+0x14e
nt!KiCommitThreadWait+0x129
nt!ExpWaitForResource+0x29f
nt!ExAcquireResourceExclusiveLite+0x1da
MYDRIVER!CtxAcquireResourceExclusive+0xda
MYDRIVER!PreCreate+0x5a0f
fltmgr!FltpPerformPreCallbacks+0x29f
fltmgr!FltpPassThroughInternal+0x8c
fltmgr!FltpCreate+0x325
nt!IopParseDevice+0x6c1
nt!ObpLookupObjectName+0x6b9
nt!ObOpenObjectByName+0x1e3
nt!NtQueryAttributesFile+0x141
nt!KiSystemServiceCopyEnd+0x13 (TrapFrame @ ffffd0002351bb00) 0x00007fff
29c6101a
THREAD ffffe001d61d9080 Cid 05a8.03cc Teb: 00007ff6d6cf0000 Win32Thread: fffff901406b3010 WAIT: (WrVirtualMemory) KernelMode Non-Alertable
ffffd00020b73200 Gate
IRP List:
ffffe001d42db530: (0006,0670) Flags: 00000404 Mdl: 00000000
Not impersonating
DeviceMap ffffc001171766d0
Owning Process ffffe001d5a35840 Image: explorer.exe
Attached Process N/A Image: N/A
Wait Start TickCount 54993 Ticks: 2395 (0:00:00:37.421)
Context Switch Count 968 IdealProcessor: 1
UserTime 00:00:00.000
KernelTime 00:00:02.265
Win32 Start Address 0x00007fff25301df0
Stack Init ffffd00020b73c90 Current ffffd00020b72ee0
Base ffffd00020b74000 Limit ffffd00020b6e000 Call 0
Priority 15 BasePriority 8 UnusualBoost 5 ForegroundBoost 2 IoPriority 2 PagePriority 2
Call Site
nt!KiSwapContext+0x76
nt!KiSwapThread+0x14e
nt!KiCommitThreadWait+0x129
nt!KeWaitForGate+0x10b
nt!MiWaitForPageWriteCompletion+0xb5
nt!MiFlushSectionInternal+0xb48
nt!MmFlushSection+0x1a2
nt!CcFlushCachePriv+0x493
nt!CcFlushCache+0x11
MYDRIVER!PreCleanup+0x2bd
fltmgr!FltpPerformPreCallbacks+0x29f
fltmgr!FltpPassThroughInternal+0x8c
fltmgr!FltpPassThrough+0x2be
fltmgr!FltpDispatch+0x9a
nt!IopCloseFile+0x12d
nt!NtClose+0x1c3
nt!KiSystemServiceCopyEnd+0x13 (TrapFrame @ ffffd00020b73b00) 0x00007fff
29c60d3a
THREAD ffffe001d2fcc880 Cid 0004.0040 Teb: 0000000000000000 Win32Thread: 0000000000000000 WAIT: (WrResource) KernelMode Non-Alertable
ffffe001d4947480 SynchronizationEvent
Not impersonating
DeviceMap ffffc0011540d010
Owning Process ffffe001d2ead040 Image: System
Attached Process N/A Image: N/A
Wait Start TickCount 57314 Ticks: 74 (0:00:00:01.156)
Context Switch Count 108 IdealProcessor: 0
UserTime 00:00:00.000
KernelTime 00:00:00.046
Win32 Start Address nt!MiMappedPageWriter (0xfffff8013e1494a4)
Stack Init ffffd000723c6c90 Current ffffd000723c6130
Base ffffd000723c7000 Limit ffffd000723c1000 Call 0
Priority 18 BasePriority 8 UnusualBoost 0 ForegroundBoost 0 IoPriority 2 PagePriority 5
Call Site
nt!KiSwapContext+0x76
nt!KiSwapThread+0x14e
nt!KiCommitThreadWait+0x129
nt!ExpWaitForResource+0x29f
nt!ExAcquireResourceExclusiveLite+0x1da
MYDRIVER!PreAcquireModWrite+0xd6
fltmgr!FltpPerformPreCallbacks+0x29f
fltmgr!FltpPreFsFilterOperation+0x2dd
nt!FsFilterPerformCallbacks+0x12a
nt!FsRtlAcquireFileForModWriteEx+0xef
nt!MiGatherMappedPages+0x284
nt!MiMappedPageWriter+0x14f
nt!PspSystemThreadStartup+0x58
nt!KiStartSystemThread+0x16
THREAD ffffe001d4b71040 Cid 0004.0af0 Teb: 0000000000000000 Win32Thread: 0000000000000000 WAIT: (WrVirtualMemory) KernelMode Non-Alertable
ffffd00072d86560 Gate
Not impersonating
DeviceMap ffffc0011540d010
Owning Process ffffe001d2ead040 Image: System
Attached Process N/A Image: N/A
Wait Start TickCount 54993 Ticks: 2395 (0:00:00:37.421)
Context Switch Count 939 IdealProcessor: 1
UserTime 00:00:00.000
KernelTime 00:00:00.687
Win32 Start Address nt!ExpWorkerThread (0xfffff8013e0af100)
Stack Init ffffd00072d86c90 Current ffffd00072d86240
Base ffffd00072d87000 Limit ffffd00072d81000 Call 0
Priority 13 BasePriority 13 UnusualBoost 0 ForegroundBoost 0 IoPriority 2 PagePriority 5
Call Site
nt!KiSwapContext+0x76
nt!KiSwapThread+0x14e
nt!KiCommitThreadWait+0x129
nt!KeWaitForGate+0x10b
nt! ?? ::FNODOBFM::`string’+0x4bdaa
nt!MiFlushSectionInternal+0x8a7
nt!MmFlushSection+0xa9
nt!CcFlushCachePriv+0x493
nt!CcWriteBehindInternal+0x17b
nt!CcWriteBehind+0x95
nt!CcWorkerThread+0x22d
nt!ExpWorkerThread+0x28c
nt!PspSystemThreadStartup+0x58
nt!KiStartSystemThread+0x16
THREAD ffffe001d61d9080 Cid 05a8.03cc Teb: 00007ff6d6cf0000 Win32Thread: fffff901406b3010 WAIT: (WrVirtualMemory) KernelMode Non-Alertable
ffffd00020b73200 Gate
IRP List:
ffffe001d42db530: (0006,0670) Flags: 00000404 Mdl: 00000000
Not impersonating
DeviceMap ffffc001171766d0
Owning Process ffffe001d5a35840 Image: explorer.exe
Attached Process N/A Image: N/A
Wait Start TickCount 54993 Ticks: 2395 (0:00:00:37.421)
Context Switch Count 968 IdealProcessor: 1
UserTime 00:00:00.000
KernelTime 00:00:02.265
Win32 Start Address 0x00007fff25301df0
Stack Init ffffd00020b73c90 Current ffffd00020b72ee0
Base ffffd00020b74000 Limit ffffd00020b6e000 Call 0
Priority 15 BasePriority 8 UnusualBoost 5 ForegroundBoost 2 IoPriority 2 PagePriority 2
Call Site
nt!KiSwapContext+0x76
nt!KiSwapThread+0x14e
nt!KiCommitThreadWait+0x129
nt!KeWaitForGate+0x10b
nt!MiWaitForPageWriteCompletion+0xb5
nt!MiFlushSectionInternal+0xb48
nt!MmFlushSection+0x1a2
nt!CcFlushCachePriv+0x493
nt!CcFlushCache+0x11
MYDRIVER!PreCleanup+0x2bd
fltmgr!FltpPerformPreCallbacks+0x29f
fltmgr!FltpPassThroughInternal+0x8c
fltmgr!FltpPassThrough+0x2be
fltmgr!FltpDispatch+0x9a
nt!IopCloseFile+0x12d
nt!NtClose+0x1c3
nt!KiSystemServiceCopyEnd+0x13 (TrapFrame @ ffffd00020b73b00) 0x00007fff
29c60d3a
THREAD ffffe001d61d9080 Cid 05a8.03cc Teb: 00007ff6d6cf0000 Win32Thread: fffff901406b3010 WAIT: (WrVirtualMemory) KernelMode Non-Alertable
ffffd00020b73200 Gate
IRP List:
ffffe001d42db530: (0006,0670) Flags: 00000404 Mdl: 00000000
Not impersonating
DeviceMap ffffc001171766d0
Owning Process ffffe001d5a35840 Image: explorer.exe
Attached Process N/A Image: N/A
Wait Start TickCount 54993 Ticks: 2395 (0:00:00:37.421)
Context Switch Count 968 IdealProcessor: 1
UserTime 00:00:00.000
KernelTime 00:00:02.265
Win32 Start Address 0x00007fff25301df0
Stack Init ffffd00020b73c90 Current ffffd00020b72ee0
Base ffffd00020b74000 Limit ffffd00020b6e000 Call 0
Priority 15 BasePriority 8 UnusualBoost 5 ForegroundBoost 2 IoPriority 2 PagePriority 2
Call Site
nt!KiSwapContext+0x76
nt!KiSwapThread+0x14e
nt!KiCommitThreadWait+0x129
nt!KeWaitForGate+0x10b
nt!MiWaitForPageWriteCompletion+0xb5
nt!MiFlushSectionInternal+0xb48
nt!MmFlushSection+0x1a2
nt!CcFlushCachePriv+0x493
nt!CcFlushCache+0x11
MYDRIVER!PreCleanup+0x2bd
fltmgr!FltpPerformPreCallbacks+0x29f
fltmgr!FltpPassThroughInternal+0x8c
fltmgr!FltpPassThrough+0x2be
fltmgr!FltpDispatch+0x9a
nt!IopCloseFile+0x12d
nt!NtClose+0x1c3
nt!KiSystemServiceCopyEnd+0x13 (TrapFrame @ ffffd00020b73b00) 0x00007fff
29c60d3a
As I said, I’m using the same locking pattern for ModWrite as I do for AcquireCc which is based on FastFat.
//---------------------
ModWrite/AcquireCc()
…
ufcb = (PFSRTL_ADVANCED_FCB_HEADER)shCtx->UpperFileObject->FsContext;
if (!ExIsResourceAcquiredSharedLite(ufcb->Resource)) {
ExAcquireResourceExclusiveLite(ufcb->Resource, TRUE);
} else {
ExAcquireResourceSharedLite(ufcb->Resource, TRUE);
}
ExAcquireResourceSharedLite(ufcb->PagingIoResource, TRUE);
//---------------------
//---------------------
PreClose()
…
ufcb = (PFSRTL_ADVANCED_FCB_HEADER)shCtx->UpperFileObject->FsContext;
CcFlushCache(shCtx->UpperFileObject->SectionObjectPointer, NULL, 0, NULL);
KeEnterCriticalRegion();
ExAcquireResourceExclusiveLite(ufcb->PagingIoResource, TRUE);
ExReleaseResourceLite(ufcb->PagingIoResource);
KeLeaveCriticalRegion();
CcPurgeCacheSection(shCtx->UpperFileObject->SectionObjectPointer, NULL, 0, FALSE);
//---------------------