[NDIS]
authorCameron Gutman <aicommander@gmail.com>
Thu, 1 Dec 2011 22:31:54 +0000 (22:31 +0000)
committerCameron Gutman <aicommander@gmail.com>
Thu, 1 Dec 2011 22:31:54 +0000 (22:31 +0000)
- Make NdisMAllocateSharedMemoryAsync and NdisMFreeSharedMemory safe to call at IRQL <= DISPATCH_LEVEL

svn path=/trunk/; revision=54557

reactos/drivers/network/ndis/include/miniport.h
reactos/drivers/network/ndis/ndis/memory.c

index a5b8c8f..3d43ee2 100644 (file)
@@ -71,7 +71,7 @@ typedef struct _MINIPORT_SHARED_MEMORY {
     BOOLEAN               Cached;
     PNDIS_MINIPORT_BLOCK  Adapter;
     PVOID                 Context;
-    PKEVENT               Event;
+    PIO_WORKITEM          WorkItem;
 } MINIPORT_SHARED_MEMORY, *PMINIPORT_SHARED_MEMORY;
 
 /* A structure of WrapperConfigurationContext (not compatible with the
index 3d96170..96ee5da 100644 (file)
@@ -188,6 +188,7 @@ NdisMAllocateSharedMemory(
 VOID
 NTAPI
 NdisMFreeSharedMemoryPassive(
+    PDEVICE_OBJECT DeviceObject,
     PVOID Context)
 /*
  * FUNCTION:  Free a common buffer
@@ -198,7 +199,6 @@ NdisMFreeSharedMemoryPassive(
  */
 {
   PMINIPORT_SHARED_MEMORY Memory = (PMINIPORT_SHARED_MEMORY)Context;
-  PRKEVENT Event = Memory->Event;
 
   NDIS_DbgPrint(MAX_TRACE, ("Called.\n"));
 
@@ -208,11 +208,8 @@ NdisMFreeSharedMemoryPassive(
       Memory->AdapterObject, Memory->Length, Memory->PhysicalAddress,
       Memory->VirtualAddress, Memory->Cached);
 
+  IoFreeWorkItem(Memory->WorkItem);
   ExFreePool(Memory);
-
-  KeSetEvent(Event,
-             IO_NO_INCREMENT,
-             FALSE);
 }
 
 \f
@@ -240,10 +237,8 @@ NdisMFreeSharedMemory(
  *       Therefore we have to do this in a worker thread.
  */
 {
-  HANDLE ThreadHandle;
   PLOGICAL_ADAPTER Adapter = (PLOGICAL_ADAPTER)MiniportAdapterHandle;
   PMINIPORT_SHARED_MEMORY Memory;
-  KEVENT Event;
 
   NDIS_DbgPrint(MAX_TRACE,("Called.\n"));
 
@@ -258,29 +253,31 @@ NdisMFreeSharedMemory(
       return;
     }
 
-  KeInitializeEvent(&Event, NotificationEvent, FALSE);
-
   Memory->AdapterObject = Adapter->NdisMiniportBlock.SystemAdapterObject;
   Memory->Length = Length;
   Memory->PhysicalAddress = PhysicalAddress;
   Memory->VirtualAddress = VirtualAddress;
   Memory->Cached = Cached;
   Memory->Adapter = &Adapter->NdisMiniportBlock;
-  Memory->Event = &Event;
 
-  PsCreateSystemThread(&ThreadHandle, THREAD_ALL_ACCESS, 0, 0, 0, NdisMFreeSharedMemoryPassive, Memory);
-  ZwClose(ThreadHandle);
+  Memory->WorkItem = IoAllocateWorkItem(Adapter->NdisMiniportBlock.DeviceObject);
+  if (!Memory->WorkItem)
+  {
+      NDIS_DbgPrint(MIN_TRACE, ("Insufficient resources\n"));
+      ExFreePool(Memory);
+      return;
+  }
 
-  KeWaitForSingleObject(&Event,
-                        Executive,
-                        KernelMode,
-                        FALSE,
-                        NULL);
+  IoQueueWorkItem(Memory->WorkItem,
+                  NdisMFreeSharedMemoryPassive,
+                  CriticalWorkQueue,
+                  Memory);
 }
 
 VOID
 NTAPI
 NdisMAllocateSharedMemoryPassive(
+    PDEVICE_OBJECT DeviceObject,
     PVOID Context)
 /*
  * FUNCTION:  Allocate a common buffer
@@ -304,6 +301,7 @@ NdisMAllocateSharedMemoryPassive(
              Memory->Adapter->MiniportAdapterContext, Memory->VirtualAddress, 
              &Memory->PhysicalAddress, Memory->Length, Memory->Context);
 
+  IoFreeWorkItem(Memory->WorkItem);
   ExFreePool(Memory);
 }
 
@@ -319,7 +317,6 @@ NdisMAllocateSharedMemoryAsync(
     IN  BOOLEAN     Cached,
     IN  PVOID       Context)
 {
-  HANDLE ThreadHandle;
   PLOGICAL_ADAPTER Adapter = (PLOGICAL_ADAPTER)MiniportAdapterHandle;
   PMINIPORT_SHARED_MEMORY Memory;
 
@@ -342,8 +339,18 @@ NdisMAllocateSharedMemoryAsync(
   Memory->Adapter = &Adapter->NdisMiniportBlock;
   Memory->Context = Context;
 
-  PsCreateSystemThread(&ThreadHandle, THREAD_ALL_ACCESS, 0, 0, 0, NdisMAllocateSharedMemoryPassive, Memory);
-  ZwClose(ThreadHandle);
+  Memory->WorkItem = IoAllocateWorkItem(Adapter->NdisMiniportBlock.DeviceObject);
+  if (!Memory->WorkItem)
+  {
+      NDIS_DbgPrint(MIN_TRACE, ("Insufficient resources\n"));
+      ExFreePool(Memory);
+      return NDIS_STATUS_FAILURE;
+  }
+
+  IoQueueWorkItem(Memory->WorkItem,
+                  NdisMAllocateSharedMemoryPassive,
+                  DelayedWorkQueue,
+                  Memory);
 
   return NDIS_STATUS_PENDING;
 }