Skip to content

Commit

Permalink
changes to zombie list. (#4145)
Browse files Browse the repository at this point in the history
  • Loading branch information
shankarseal authored Jan 18, 2025
1 parent 149cba7 commit b01b310
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 41 deletions.
4 changes: 3 additions & 1 deletion netebpfext/net_ebpf_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,9 @@ net_ebpf_extension_wfp_filter_context_create(
local_filter_context = NULL;

Exit:
CLEAN_UP_FILTER_CONTEXT(local_filter_context);
if (local_filter_context != NULL) {
CLEAN_UP_FILTER_CONTEXT(local_filter_context);
}

NET_EBPF_EXT_RETURN_RESULT(result);
}
Expand Down
48 changes: 15 additions & 33 deletions netebpfext/net_ebpf_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,34 +150,23 @@ typedef struct _net_ebpf_extension_wfp_filter_context
#define PRAGMA_WARNING_SUPPRESS_26100 _Pragma("warning(suppress: 26100)")
#define PRAGMA_WARNING_POP _Pragma("warning(pop)")

#define CLEAN_UP_FILTER_CONTEXT_COMMON(filter_context) \
if ((filter_context)->filter_ids != NULL) { \
ExFreePool((filter_context)->filter_ids); \
} \
PRAGMA_WARNING_PUSH \
PRAGMA_WARNING_SUPPRESS_26100 \
if ((filter_context)->client_contexts != NULL) { \
ExFreePool((filter_context)->client_contexts); \
} \
PRAGMA_WARNING_POP \
if ((filter_context)->wfp_engine_handle != NULL) { \
FwpmEngineClose((filter_context)->wfp_engine_handle); \
} \
#define CLEAN_UP_FILTER_CONTEXT(filter_context) \
ASSERT((filter_context) != NULL); \
net_ebpf_extension_hook_provider_remove_filter_context_from_zombie_list((filter_context)); \
if ((filter_context)->filter_ids != NULL) { \
ExFreePool((filter_context)->filter_ids); \
} \
PRAGMA_WARNING_PUSH \
PRAGMA_WARNING_SUPPRESS_26100 \
if ((filter_context)->client_contexts != NULL) { \
ExFreePool((filter_context)->client_contexts); \
} \
PRAGMA_WARNING_POP \
if ((filter_context)->wfp_engine_handle != NULL) { \
FwpmEngineClose((filter_context)->wfp_engine_handle); \
} \
ExFreePool((filter_context));

#if !defined(NDEBUG)
#define CLEAN_UP_FILTER_CONTEXT(filter_context) \
if ((filter_context) != NULL) { \
net_ebpf_ext_remove_filter_context_from_zombie_list((filter_context)); \
CLEAN_UP_FILTER_CONTEXT_COMMON(filter_context); \
}
#else
#define CLEAN_UP_FILTER_CONTEXT(filter_context) \
if ((filter_context) != NULL) { \
CLEAN_UP_FILTER_CONTEXT_COMMON(filter_context); \
}
#endif

#define REFERENCE_FILTER_CONTEXT(filter_context) \
if ((filter_context) != NULL) { \
InterlockedIncrement(&(filter_context)->reference_count); \
Expand Down Expand Up @@ -411,11 +400,4 @@ net_ebpf_ext_add_client_context(
void
net_ebpf_ext_add_filter_context_to_zombie_list(_Inout_ net_ebpf_extension_wfp_filter_context_t* filter_context);

/**
* @brief Remove the filter context from the zombie list.
*
* @param filter_context Filter context to remove from the zombie list.
*/
void
net_ebpf_ext_remove_filter_context_from_zombie_list(_Inout_ net_ebpf_extension_wfp_filter_context_t* filter_context);
#endif
4 changes: 3 additions & 1 deletion netebpfext/net_ebpf_ext_bind.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ _net_ebpf_ext_bind_create_filter_context(
local_filter_context = NULL;

Exit:
CLEAN_UP_FILTER_CONTEXT(local_filter_context);
if (local_filter_context != NULL) {
CLEAN_UP_FILTER_CONTEXT(local_filter_context);
}

NET_EBPF_EXT_RETURN_RESULT(result);
}
Expand Down
26 changes: 21 additions & 5 deletions netebpfext/net_ebpf_ext_hook_provider.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ typedef struct _net_ebpf_extension_hook_provider
const void* custom_data; ///< Opaque pointer to hook specific data associated for this provider.
_Guarded_by_(lock)
LIST_ENTRY filter_context_list; ///< Linked list of filter contexts that are attached to this provider.
EX_PUSH_LOCK zombie_list_lock; ///< Lock for zombie filters list.
_Guarded_by_(zombie_list_lock)
LIST_ENTRY zombie_filter_context_list; ///< Linked list of filter contexts that are attached to this provider.
} net_ebpf_extension_hook_provider_t;

typedef struct _net_ebpf_extension_invoke_programs_parameters
Expand Down Expand Up @@ -612,11 +615,10 @@ _Requires_exclusive_lock_held_(provider_context->lock) static void _net_ebpf_ext

// Remove the list entry from the provider's list of filter contexts.
RemoveEntryList(&filter_context->link);

#if !defined(NDEBUG)
// Add the entry to the zombie list (for debugging purposes)
net_ebpf_ext_add_filter_context_to_zombie_list(filter_context);
#endif
// Insert the list entry to the list of zombie filter contexts.
ACQUIRE_PUSH_LOCK_EXCLUSIVE(&provider_context->zombie_list_lock);
InsertTailList(&provider_context->zombie_filter_context_list, &filter_context->link);
RELEASE_PUSH_LOCK_EXCLUSIVE(&provider_context->zombie_list_lock);

// Release the filter context.
provider_context->dispatch.delete_filter_context(filter_context);
Expand Down Expand Up @@ -741,6 +743,8 @@ net_ebpf_extension_hook_provider_register(
memset(local_provider_context, 0, sizeof(net_ebpf_extension_hook_provider_t));
ExInitializePushLock(&local_provider_context->lock);
InitializeListHead(&local_provider_context->filter_context_list);
ExInitializePushLock(&local_provider_context->zombie_list_lock);
InitializeListHead(&local_provider_context->zombie_filter_context_list);

characteristics = &local_provider_context->characteristics;
characteristics->Length = sizeof(NPI_PROVIDER_CHARACTERISTICS);
Expand Down Expand Up @@ -781,3 +785,15 @@ net_ebpf_extension_hook_provider_register(

NET_EBPF_EXT_RETURN_NTSTATUS(status);
}

void
net_ebpf_extension_hook_provider_remove_filter_context_from_zombie_list(
_Inout_ net_ebpf_extension_wfp_filter_context_t* filter_context)
{
net_ebpf_extension_hook_provider_t* local_provider_context =
(net_ebpf_extension_hook_provider_t*)filter_context->provider_context;
ASSERT(local_provider_context != NULL);
ACQUIRE_PUSH_LOCK_EXCLUSIVE(&local_provider_context->zombie_list_lock);
RemoveEntryList(&filter_context->link);
RELEASE_PUSH_LOCK_EXCLUSIVE(&local_provider_context->zombie_list_lock);
}
11 changes: 10 additions & 1 deletion netebpfext/net_ebpf_ext_hook_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,13 @@ net_ebpf_extension_hook_expand_stack_and_invoke_programs(
* @return Attach capability for the hook provider.
*/
net_ebpf_extension_hook_attach_capability_t
net_ebpf_extension_hook_provider_get_attach_capability(_In_ const net_ebpf_extension_hook_provider_t* provider_context);
net_ebpf_extension_hook_provider_get_attach_capability(_In_ const net_ebpf_extension_hook_provider_t* provider_context);

/**
* @brief Remove the filter context from the zombie list.
*
* @param filter_context Filter context to remove from the zombie list.
*/
void
net_ebpf_extension_hook_provider_remove_filter_context_from_zombie_list(
_Inout_ net_ebpf_extension_wfp_filter_context_t* filter_context);

0 comments on commit b01b310

Please sign in to comment.