From f6f25eec8fae500dac179acac3c86f9963239e78 Mon Sep 17 00:00:00 2001 From: Mounir IDRASSI Date: Mon, 8 Sep 2025 12:14:20 +0900 Subject: [PATCH] Windows driver: simplify TCSleep to use KeDelayExecutionThread Replace timer-based TCSleep (which allocated a KTIMER and waited on it) with an implementation that calls KeDelayExecutionThread. This removes dynamic allocation and kernel timer usage to simplify the code and reduce resource overhead. Adds an IRQL <= APC_LEVEL assertion and documents the requirement. This is safe because TCSleep is always called from code that runs at PASSIVE_LEVEL --- src/Driver/Ntdriver.c | 20 ++++++-------------- src/Driver/Ntdriver.h | 2 +- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/src/Driver/Ntdriver.c b/src/Driver/Ntdriver.c index eb45d41d..8b464ce0 100644 --- a/src/Driver/Ntdriver.c +++ b/src/Driver/Ntdriver.c @@ -2992,21 +2992,13 @@ void TCStopVolumeThread (PDEVICE_OBJECT DeviceObject, PEXTENSION Extension) // Suspend current thread for a number of milliseconds -void TCSleep (int milliSeconds) +// Must be called at IRQL <= APC_LEVEL +VOID TCSleep(ULONG milliSeconds) { - PKTIMER timer = (PKTIMER) TCalloc (sizeof (KTIMER)); - LARGE_INTEGER duetime; - - if (!timer) - return; - - duetime.QuadPart = (__int64) milliSeconds * -10000; - KeInitializeTimerEx(timer, NotificationTimer); - KeSetTimerEx(timer, duetime, 0, NULL); - - KeWaitForSingleObject (timer, Executive, KernelMode, FALSE, NULL); - - TCfree (timer); + LARGE_INTEGER interval; + interval.QuadPart = -(LONGLONG)milliSeconds * 10000; // 100 ns units + ASSERT(KeGetCurrentIrql() <= APC_LEVEL); + (void)KeDelayExecutionThread(KernelMode, FALSE, &interval); } BOOL IsDeviceName(wchar_t wszVolume[TC_MAX_PATH]) diff --git a/src/Driver/Ntdriver.h b/src/Driver/Ntdriver.h index 0c440ba5..82253146 100644 --- a/src/Driver/Ntdriver.h +++ b/src/Driver/Ntdriver.h @@ -151,7 +151,7 @@ NTSTATUS TCStartVolumeThread (PDEVICE_OBJECT DeviceObject, PEXTENSION Extension, void TCStopThread (PKTHREAD kThread, PKEVENT wakeUpEvent); void TCStopVolumeThread (PDEVICE_OBJECT DeviceObject, PEXTENSION Extension); VOID VolumeThreadProc (PVOID Context); -void TCSleep (int milliSeconds); +void TCSleep (ULONG milliSeconds); void TCGetNTNameFromNumber (LPWSTR ntname, int cbNtName, int nDriveNo); void TCGetDosNameFromNumber (LPWSTR dosname, int cbDosName, int nDriveNo, DeviceNamespaceType namespaceType); LPWSTR TCTranslateCode (ULONG ulCode);