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

Perf event array map kernel-side implementation #4144

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

mikeagun
Copy link
Contributor

@mikeagun mikeagun commented Jan 16, 2025

Description

Implements the kernel side of perf event array maps.

This includes the core and kernel changes for #658, with part of the user-side API skeleton.

Testing

Includes platform and execution context tests.

Documentation

Documented in code and PerfEventArray.md.

Installation

N/A

@mikeagun mikeagun marked this pull request as ready for review February 4, 2025 19:58
@mikeagun mikeagun changed the title DRAFT Perf event array map implementation. Perf event array map kernel-side implementation. Feb 4, 2025
@mikeagun
Copy link
Contributor Author

Moving to draft until the ring buffer refactoring is merged in #4204

@mikeagun mikeagun marked this pull request as draft February 12, 2025 21:54
@mikeagun
Copy link
Contributor Author

This is currently blocked on #4204, as it uses the new ring buffer for wait-free reserve (at dispatch) in the per-cpu rings. Most of the refactoring to use the new ring buffer has been done, but after the new ring buffer implementation is merged I'll update+publish this again.

Michael Agun added 2 commits March 7, 2025 08:08
- Makes header support mandatory for programs and extensions.
- Completes context header support for netebpfext
- Adds context header support to all test/mock bpf program contexts.
@mikeagun
Copy link
Contributor Author

mikeagun commented Mar 7, 2025

I split the context header support requirement out into #4267. After that passes all CICD tests I'll rebase this PR split into two commits - context header support then perf event array impl.

@mikeagun mikeagun changed the title Perf event array map kernel-side implementation. Perf event array map kernel-side implementation Mar 7, 2025
@@ -101,17 +101,17 @@ structure from provided data and context buffers.
context structure and populates the returned data and context buffers.
* `required_irql`: IRQL at which the eBPF program is invoked by bpf_prog_test_run_opts.
* `capabilities`: 32-bit integer describing the optional capabilities / features supported by the extension.
* `supports_context_header`: Flag indicating extension supports adding a context header at the start of each context passed to the eBPF program.
* `supports_context_header`: Required flag indicating extension supports adding a context header at the start of each context passed to the eBPF program. This flag must be set.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The context header changes have been split out into PR #4267

This comment is to ensure 4267 merges before this PR (this PR contains the context header changes as the first commit).

@@ -0,0 +1,147 @@
// Copyright (c) eBPF for Windows contributors
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we need this as a separate module? Why not merge all of this into ebpf_core_perf_event_array_map_t ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was easier to thoroughly test with the layers separated. I agree that the consolidation makes more sense at this point.

The perf event array on linux is significantly different at the mmap level (though very similar at libbpf callback level we currently expose), but with the refactoring to make perf event array an array of ebpf_ring_buffer_t for now it doesn't make sense to add a new set of files for it.

The PR has been updated to remove ebpf_perf_event_array.c/h

@@ -302,6 +319,32 @@ _ring_next_consumer_record(_In_ ebpf_ring_buffer_t* ring, _When_(return != NULL,
return NULL;
}

_Must_inspect_result_ ebpf_result_t
ebpf_ring_buffer_initialize_ring(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since there is both a create and a initialize function, ideally create should allocate memory, and initialize should populate it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

create allocates memory for an ebpf_ring_buffer_t and initializes it.

  • the ring buffer map uses this (as it did before) to create the ebpf_ring_buffer_t that the core ring buffer map object points to

initialize initializes an ebpf_ring_buffer_t struct in-place (including allocating the memory ring).

  • perf event array map uses this to initialize the ebpf_ring_buffer_t that is inside the ebpf_core_perf_ring_t

}

_Must_inspect_result_ ebpf_result_t
ebpf_perf_event_array_output_simple(
Copy link
Collaborator

@shankarseal shankarseal Mar 10, 2025

Choose a reason for hiding this comment

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

Is this wrapper function really needed?

Copy link
Contributor Author

@mikeagun mikeagun Mar 11, 2025

Choose a reason for hiding this comment

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

goto Exit;
}
result =
ebpf_perf_event_output_simple(map, (uint32_t)EBPF_MAP_FLAG_CURRENT_CPU, (uint8_t*)request->data, data_length);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Simply call ebpf_perf_event_output with ctx = NULL?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is invalid to call perf_event_output (the helper) without ctx, so I added a separate function to simply write a single data blob to a perf event array.

The difference between the two functions is now larger and more obvious -- mainly that output_simple can skip most of the argument handling and doesn't require ctx/flags.

Do you still think I should remove this (and then allow and check for null ctx for ebpf_perf_event_output)?

uint32_t cpu_id;
ebpf_perf_event_array_map_async_query_result_t* async_query_result;
void* async_context;
} ebpf_core_perf_event_array_map_async_query_context_t;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe this can be reconciled with ebpf_core_ring_buffer_map_async_query_context_t in a subsequent pull request.

@shankarseal shankarseal linked an issue Mar 10, 2025 that may be closed by this pull request
ebpf_map_create(&map_name, &map_definition, (uintptr_t)ebpf_handle_invalid, &local_map) == EBPF_SUCCESS);
map.reset(local_map);
}
// TODO (before merge): implementme
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment to track porting the necessary platform unit tests over to execution context unit tests using the new ebpf_maps based definition (vs. the old ebpf_perf_event_array based implementation).

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.

eBPF for Windows should support perf_event_array
4 participants