VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Driver
diff options
context:
space:
mode:
authorMounir IDRASSI <mounir.idrassi@idrix.fr>2024-11-22 15:19:10 +0100
committerMounir IDRASSI <mounir.idrassi@idrix.fr>2024-11-22 15:19:10 +0100
commit5a85c54c6ef556b96cc1ed844620a4ccee1b7837 (patch)
treef4e9023cd331fa1218eed76351b3bf9aa33333c7 /src/Driver
parent94903363572cbc8cf829b66591752a684f8361dc (diff)
downloadVeraCrypt-5a85c54c6ef556b96cc1ed844620a4ccee1b7837.tar.gz
VeraCrypt-5a85c54c6ef556b96cc1ed844620a4ccee1b7837.zip
Windows Driver: Optimize spinlock usage in CompleteIrpWorkItemRoutine
Reduce the critical section protected by spinlock to only cover the list manipulation operation. Move the ActiveWorkItems counter decrement outside the spinlock using InterlockedDecrement, and separate event signaling from the locked section. This change minimizes time spent at raised IRQL (DISPATCH_LEVEL) and reduces potential for lock contention.
Diffstat (limited to 'src/Driver')
-rw-r--r--src/Driver/EncryptedIoQueue.c12
1 files changed, 4 insertions, 8 deletions
diff --git a/src/Driver/EncryptedIoQueue.c b/src/Driver/EncryptedIoQueue.c
index 85d7ccff..d68c3f09 100644
--- a/src/Driver/EncryptedIoQueue.c
+++ b/src/Driver/EncryptedIoQueue.c
@@ -271,6 +271,7 @@ static VOID CompleteIrpWorkItemRoutine(PDEVICE_OBJECT DeviceObject, PVOID Contex
PCOMPLETE_IRP_WORK_ITEM workItem = (PCOMPLETE_IRP_WORK_ITEM)Context;
EncryptedIoQueueItem* item = (EncryptedIoQueueItem * ) workItem->Item;
EncryptedIoQueue* queue = item->Queue;
+ KIRQL oldIrql;
UNREFERENCED_PARAMETER(DeviceObject);
__try
@@ -283,19 +284,14 @@ static VOID CompleteIrpWorkItemRoutine(PDEVICE_OBJECT DeviceObject, PVOID Contex
}
__finally
{
- // Return the work item to the free list
- KIRQL oldIrql;
- KeAcquireSpinLock(&queue->WorkItemLock, &oldIrql);
-
- // Decrement ActiveWorkItems
- LONG activeWorkItems = InterlockedDecrement(&queue->ActiveWorkItems);
-
// If no active work items remain, signal the event
- if (activeWorkItems == 0)
+ if (InterlockedDecrement(&queue->ActiveWorkItems) == 0)
{
KeSetEvent(&queue->NoActiveWorkItemsEvent, IO_NO_INCREMENT, FALSE);
}
+ // Return the work item to the free list
+ KeAcquireSpinLock(&queue->WorkItemLock, &oldIrql);
InsertTailList(&queue->FreeWorkItemsList, &workItem->ListEntry);
KeReleaseSpinLock(&queue->WorkItemLock, oldIrql);