Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement kevent #96

Merged
merged 2 commits into from
Aug 28, 2023
Merged

Implement kevent #96

merged 2 commits into from
Aug 28, 2023

Conversation

Alan-Jowett
Copy link
Member

Note:
This implementation permits using KEVENT as a local stack variable without leaking large numbers of Win32 handles.

It is a prerequisite to fixing microsoft/ebpf-for-windows#2787.

Signed-off-by: Alan Jowett <[email protected]>
SynchronizationEvent
} EVENT_TYPE;

typedef struct _kevent
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this not an abstraction for a HANDLE for a simple event in usermode?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because kernel mode events aren't handles that require a call to CloseHandle on. Valid kernel mode code can initialize events without calling any close API on them.

As an example, this is valid, but it we use a simple Win32 event via a handle it leaks handles.

/**
 * @brief Send a message to the specified CPU and wait for it to complete.
 *
 * @param[in] message Message to send.
 * @param[in] cpu_id CPU to send the message to.
 */
_IRQL_requires_max_(APC_LEVEL) static void _ebpf_epoch_send_message_and_wait(
    _In_ ebpf_epoch_cpu_message_t* message, uint32_t cpu_id)
{
    KDPC dpc;

    // Initialize the completion event.
    KeInitializeEvent(&message->completion_event, NotificationEvent, FALSE);

    // Initialize the dpc
    KeInitializeDpc(&dpc, _ebpf_epoch_messenger_worker, NULL);
    KeSetTargetProcessorDpc(&dpc, (uint8_t)cpu_id);

    // Send the message.
    KeInsertQueueDpc(&dpc, message, NULL);

    // Wait for the message to complete.
    KeWaitForSingleObject(&message->completion_event, Executive, KernelMode, FALSE, NULL);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Alan-Jowett semaphores have the same difference between user and kernel mode and the usersim implementation of semaphores does use handles. See usersim_free_semaphores() which is called by usersim_platform_terminate().

inc/usersim/ke.h Outdated Show resolved Hide resolved
inc/usersim/ke.h Outdated Show resolved Hide resolved
inc/usersim/ke.h Outdated Show resolved Hide resolved
src/ke.cpp Outdated Show resolved Hide resolved
src/ke.cpp Outdated Show resolved Hide resolved
src/ke.cpp Outdated Show resolved Hide resolved
Signed-off-by: Alan Jowett <[email protected]>
@Alan-Jowett Alan-Jowett merged commit ff7327b into microsoft:main Aug 28, 2023
5 checks passed
@Alan-Jowett Alan-Jowett deleted the implemen_kevent branch August 28, 2023 14:08
@Alan-Jowett Alan-Jowett restored the implemen_kevent branch August 29, 2023 19:57
@Alan-Jowett Alan-Jowett deleted the implemen_kevent branch March 25, 2024 16:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants