Wait for event signalling from Kernel mode to User mode

Hi folks,
I am newbie and currently working on notifying User mode thread from KMDF driver. From what I have read, I need to use Inverted IOCTL mechanism to notify the user mode thread from KMDF.
Following is the sequence that I am following:

  1. Create user mode thread with overlapped flag
  2. Create an event that will be used in hevent member of overlap structure(manual reset, non signalled).
  3. Initialise the overlap structure. Also initialize the hevent flag with event created in step 2.
  4. Issue an IOCTL(also pass the overlap structure).
  5. Wait for the event to be signalled using GetOverlappedResultEx with timeout parameter to be INFINITE.

My question is , when the IOCTL is completed by the handler using WdfRequestComplete, do I need to explicitly set the event in order to signal the waiting thread, or will the user mode thread be automatically signalled once IOCTL is complete

The first thing to remember is that the inverted call pattern can be used with any UM IO model. That’s not quite true, but they are separate concepts.

Inverted call refers to the idea of having UM components (usually services) open HANDLEs to KM components. Normally, the KM components wait for work to do from UM, but inverted call works the other way where UM waits for work from KM. It usually sends the results back to KM in some fashion

UM IO models are closely tied to threading models in program design. Theoretically, any of the IO models supported by Windows can work with Inverted call, but in practice some work better than others. OVERLAPPED IO is highly desirable because of the ability to send several requests from UM to KM in advance of there being any work to do. These requests do not complete immediately, but are kept in a pending state until there is work that KM needs UM to do. This helps to avoid the problem that while KM needs UM to do the work, it can’t force UM to ask for it and usually can’t buffer the work for very long. By using OVERLAPPED IO, UM will pend several requests for work with the hope that in KM, there will always be something to use right away

One way that a UM program can determine that an OVERLAPPED IO has completed is by including an EVENT object in the OVERLAPPED struct used for the DeviceIoControl, ReadFile, or WriteFile call being used. You can use GetOverlappedResult, HasOverlappedIoCompleted or user mode APCs, but by far the best is IOCP. For new development the thread pool APIs are recommended

The main point is that your KM component need not concern itself with how the UM component waits for IO completion and you don’t need to do anything special whatever method is chosen. on the KM side, the big problem is handling the case where there are no pending calls to complete. Should the data be discarded? buffered in some way? and if so then for how long or how much

when the IOCTL is completed by the handler using WdfRequestComplete, do I need to explicitly set the event in order to signal the waiting thread, or will the user mode thread be automatically signalled once IOCTL is complete

The event will automatically be signaled. You do not have to do anything other than completing the I/O Request.

1 Like