diff options
| author | Yang You (UU) <[email protected]> | 2025-12-04 15:30:32 -0800 |
|---|---|---|
| committer | Yang You (UU) <[email protected]> | 2025-12-04 15:30:32 -0800 |
| commit | 10835ebc2bc4a774907fb47880ffc366b159622c (patch) | |
| tree | 7cfd1d9508a9a9c216706269a37cddb9db61299c | |
| parent | 977cb2461482a898f96ee08f9cb24eb7f421da03 (diff) | |
Cleanup the KSpinlock.h and replace with WDFSpinlock
5 files changed, 33 insertions, 168 deletions
diff --git a/network/netadaptercx/netvadapterlibrary/code/enl.cpp b/network/netadaptercx/netvadapterlibrary/code/enl.cpp index 30822900..4087372d 100644 --- a/network/netadaptercx/netvadapterlibrary/code/enl.cpp +++ b/network/netadaptercx/netvadapterlibrary/code/enl.cpp @@ -241,13 +241,15 @@ enlpCheckAndArmQueue( // arm it. if (ringBuffer->BeginIndex == ringBuffer->EndIndex) { - KAcquireSpinLock lock(Q->Spinlock); + WdfSpinLockAcquire(Q->Spinlock); if (ringBuffer->BeginIndex == ringBuffer->EndIndex) { armed = TRUE; Q->Armed = TRUE; } + + WdfSpinLockRelease(Q->Spinlock); } return armed; @@ -543,6 +545,7 @@ EnlCreateQueue( ENLP_LINK* enlLink; ENLP_PORT* port; ENLP_QUEUE* enlQueue; + NTSTATUS status; if (Tx) // Tx { @@ -580,6 +583,15 @@ EnlCreateQueue( &netvRxQueue->m_adapter, reinterpret_cast<ULONG_PTR>(Queue), netvRxQueue); } + // Create WDF spinlock for the queue, parent to the queue's WDF handle + { + WDF_OBJECT_ATTRIBUTES attributes; + WDF_OBJECT_ATTRIBUTES_INIT(&attributes); + attributes.ParentObject = enlQueue->Queue->m_handle; + status = WdfSpinLockCreate(&attributes, &enlQueue->Spinlock); + NT_ASSERT(NT_SUCCESS(status)); + } + EnlpResumeThread(&enlLink->EnlThread); return enlQueue; @@ -615,15 +627,16 @@ EnlRingDoorBell( ) { InterlockedExchange((volatile LONG *)&Queue->QueueEnd, (LONG)EndIndex); - - KAcquireSpinLock lock{ Queue->Spinlock }; + WdfSpinLockAcquire(Queue->Spinlock); if (Queue->Armed) { NT_ASSERT(Queue->ArmWaitEvent != NULL); Queue->Armed = FALSE; - lock.Release(); + WdfSpinLockRelease(Queue->Spinlock); Queue->ArmWaitEvent->Set(); + return; } + WdfSpinLockRelease(Queue->Spinlock); } _IRQL_requires_max_(PASSIVE_LEVEL) diff --git a/network/netadaptercx/netvadapterlibrary/code/enl.h b/network/netadaptercx/netvadapterlibrary/code/enl.h index 529c36ac..41f8d11d 100644 --- a/network/netadaptercx/netvadapterlibrary/code/enl.h +++ b/network/netadaptercx/netvadapterlibrary/code/enl.h @@ -27,7 +27,6 @@ #endif #include "rtl/KWaitEvent.h" -#include "rtl/KSpinLock.h" #define ENL_MAX_PROC_COUNT 16 #define ENLP_PORT_COUNT 2 @@ -209,7 +208,7 @@ struct ENLP_QUEUE ENLP_PORT * EnlPortHandle{}; BOOLEAN Armed{}; - KSpinLock Spinlock{}; + WDFSPINLOCK Spinlock{}; KAutoEvent *ArmWaitEvent{}; ENL_QUEUE_STATE State{}; diff --git a/network/netadaptercx/netvadapterlibrary/code/memory.cpp b/network/netadaptercx/netvadapterlibrary/code/memory.cpp index 0c010d59..b7536531 100644 --- a/network/netadaptercx/netvadapterlibrary/code/memory.cpp +++ b/network/netadaptercx/netvadapterlibrary/code/memory.cpp @@ -34,6 +34,13 @@ Memory::Initialize( } m_lastBufferToUse = PREALLOCATED_BUFFERS_COUNT; + // Create WDF spinlock for the queue, parent to the queue's WDF handle + { + WDF_OBJECT_ATTRIBUTES attributes; + WDF_OBJECT_ATTRIBUTES_INIT(&attributes); + status = WdfSpinLockCreate(&attributes, &m_spinLock); + NT_ASSERT(NT_SUCCESS(status)); + } return STATUS_SUCCESS; } @@ -56,14 +63,17 @@ Memory::PopAvailableBuffer( void ) { - KAcquireSpinLock lock{ m_spinLock }; + WdfSpinLockAcquire(m_spinLock); if (m_lastBufferToUse == 0) { + WdfSpinLockRelease(m_spinLock); return nullptr; } + auto returnbuffer = m_buffersReadyToUse[--m_lastBufferToUse]; + WdfSpinLockRelease(m_spinLock); - return m_buffersReadyToUse[--m_lastBufferToUse]; + return returnbuffer; } void @@ -71,11 +81,12 @@ Memory::ReturnBuffer( MemoryBuffer* Buffer ) { - KAcquireSpinLock lock{ m_spinLock }; + WdfSpinLockAcquire(m_spinLock); NT_FRE_ASSERT(m_lastBufferToUse < PREALLOCATED_BUFFERS_COUNT); m_buffersReadyToUse[m_lastBufferToUse++] = Buffer; + WdfSpinLockRelease(m_spinLock); } #endif //NETCX 2.6 only diff --git a/network/netadaptercx/netvadapterlibrary/code/memory.h b/network/netadaptercx/netvadapterlibrary/code/memory.h index a07af6b8..4c19fc39 100644 --- a/network/netadaptercx/netvadapterlibrary/code/memory.h +++ b/network/netadaptercx/netvadapterlibrary/code/memory.h @@ -1,6 +1,5 @@ // Copyright (C) Microsoft Corporation. All rights reserved. #pragma once -#include <KSpinLock.h> #define MAX_RX_BUFFER_SIZE 65535 @@ -55,7 +54,7 @@ private: size_t m_lastBufferToUse; - KSpinLock + WDFSPINLOCK m_spinLock{}; }; diff --git a/network/netadaptercx/netvadapterlibrary/code/rtl/KSpinLock.h b/network/netadaptercx/netvadapterlibrary/code/rtl/KSpinLock.h deleted file mode 100644 index c344e7bb..00000000 --- a/network/netadaptercx/netvadapterlibrary/code/rtl/KSpinLock.h +++ /dev/null @@ -1,157 +0,0 @@ -#pragma once - -#include <KMacros.h> -#define WIN_ASSERT NT_ASSERT - -class KSpinLockBase -{ -protected: - - KSpinLockBase() = default; - - void InitializeBase() - { -#if _KERNEL_MODE - KeInitializeSpinLock(&m_lock); -#else - InitializeSRWLock(&m_lock); -#endif - } - -public: - - KSpinLockBase(KSpinLockBase&) = delete; - KSpinLockBase(KSpinLockBase&&) = delete; - KSpinLockBase& operator=(KSpinLockBase&) = delete; - KSpinLockBase& operator=(KSpinLockBase&&) = delete; - - _Requires_lock_not_held_(*this) - _Acquires_lock_(*this) - _IRQL_requires_max_(DISPATCH_LEVEL) - _IRQL_saves_ - _IRQL_raises_(DISPATCH_LEVEL) - KIRQL Acquire() - { -#if _KERNEL_MODE - KIRQL oldIrql; - KeAcquireSpinLock(&m_lock, &oldIrql); - return oldIrql; -#else - AcquireSRWLockExclusive(&m_lock); - return 0; -#endif - } - - _Requires_lock_held_(*this) - _Releases_lock_(*this) - _IRQL_requires_(DISPATCH_LEVEL) - void Release(_In_ KIRQL oldIrql) - { -#if _KERNEL_MODE - return KeReleaseSpinLock(&m_lock, oldIrql); -#else - UNREFERENCED_PARAMETER(oldIrql); - ReleaseSRWLockExclusive(&m_lock); -#endif - } - - _IRQL_requires_max_(DISPATCH_LEVEL) - bool IsAcquired() - { -#if _KERNEL_MODE - return !KeTestSpinLock(&m_lock); -#else - if (!TryAcquireSRWLockExclusive(&m_lock)) - return false; - - ReleaseSRWLockExclusive(&m_lock); - return true; -#endif - } - -private: - -#if _KERNEL_MODE - KSPIN_LOCK m_lock; -#else - SRWLOCK m_lock; -#endif -}; - -class KSpinLock : public KSpinLockBase -{ -public: - - KSpinLock() - { - InitializeBase(); - } -}; - -class KSpinLockManualConstruct : public KSpinLockBase -{ -public: - - KSpinLockManualConstruct() = default; - - void Initialize() - { - InitializeBase(); - } -}; - -class KAcquireSpinLock -{ -public: - - _Requires_lock_not_held_(lock) - _Acquires_lock_(lock) - _IRQL_requires_max_(DISPATCH_LEVEL) - _IRQL_raises_(DISPATCH_LEVEL) - KAcquireSpinLock(KSpinLockBase &lock) : - m_lock(lock) - { - Acquire(); - } - - _Requires_lock_held_(this->m_lock) - _Releases_lock_(this->m_lock) - _IRQL_requires_(DISPATCH_LEVEL) - ~KAcquireSpinLock() - { - if (IsAcquired()) - Release(); - } - - _Requires_lock_not_held_(this->m_lock) - _Acquires_lock_(this->m_lock) - _IRQL_requires_max_(DISPATCH_LEVEL) - _IRQL_raises_(DISPATCH_LEVEL) - void Acquire() - { - WIN_ASSERT(!IsAcquired()); - m_oldIrql = m_lock.Acquire(); - } - - _Requires_lock_held_(this->m_lock) - _Releases_lock_(this->m_lock) - _IRQL_requires_(DISPATCH_LEVEL) - void Release() - { - WIN_ASSERT(IsAcquired()); - m_lock.Release(m_oldIrql); - m_oldIrql = NOT_ACQUIRED; - } - - _IRQL_requires_max_(DISPATCH_LEVEL) - bool IsAcquired() - { - return m_oldIrql != NOT_ACQUIRED; - } - -private: - - static const KIRQL NOT_ACQUIRED = (KIRQL)-1; - KIRQL m_oldIrql = NOT_ACQUIRED; - KSpinLockBase &m_lock; -}; |
