Commit 792caccc authored by Song Liu's avatar Song Liu Committed by Alexei Starovoitov

bpf: Introduce BPF_F_PRESERVE_ELEMS for perf event array

Currently, perf event in perf event array is removed from the array when
the map fd used to add the event is closed. This behavior makes it
difficult to the share perf events with perf event array.

Introduce perf event map that keeps the perf event open with a new flag
BPF_F_PRESERVE_ELEMS. With this flag set, perf events in the array are not
removed when the original map fd is closed. Instead, the perf event will
stay in the map until 1) it is explicitly removed from the array; or 2)
the array is freed.
Signed-off-by: default avatarSong Liu <songliubraving@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200930224927.1936644-2-songliubraving@fb.com
parent 3effc06a
...@@ -414,6 +414,9 @@ enum { ...@@ -414,6 +414,9 @@ enum {
/* Enable memory-mapping BPF map */ /* Enable memory-mapping BPF map */
BPF_F_MMAPABLE = (1U << 10), BPF_F_MMAPABLE = (1U << 10),
/* Share perf_event among processes */
BPF_F_PRESERVE_ELEMS = (1U << 11),
}; };
/* Flags for BPF_PROG_QUERY. */ /* Flags for BPF_PROG_QUERY. */
......
...@@ -15,7 +15,8 @@ ...@@ -15,7 +15,8 @@
#include "map_in_map.h" #include "map_in_map.h"
#define ARRAY_CREATE_FLAG_MASK \ #define ARRAY_CREATE_FLAG_MASK \
(BPF_F_NUMA_NODE | BPF_F_MMAPABLE | BPF_F_ACCESS_MASK) (BPF_F_NUMA_NODE | BPF_F_MMAPABLE | BPF_F_ACCESS_MASK | \
BPF_F_PRESERVE_ELEMS)
static void bpf_array_free_percpu(struct bpf_array *array) static void bpf_array_free_percpu(struct bpf_array *array)
{ {
...@@ -64,6 +65,10 @@ int array_map_alloc_check(union bpf_attr *attr) ...@@ -64,6 +65,10 @@ int array_map_alloc_check(union bpf_attr *attr)
attr->map_flags & BPF_F_MMAPABLE) attr->map_flags & BPF_F_MMAPABLE)
return -EINVAL; return -EINVAL;
if (attr->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY &&
attr->map_flags & BPF_F_PRESERVE_ELEMS)
return -EINVAL;
if (attr->value_size > KMALLOC_MAX_SIZE) if (attr->value_size > KMALLOC_MAX_SIZE)
/* if value_size is bigger, the user space won't be able to /* if value_size is bigger, the user space won't be able to
* access the elements. * access the elements.
...@@ -1134,6 +1139,9 @@ static void perf_event_fd_array_release(struct bpf_map *map, ...@@ -1134,6 +1139,9 @@ static void perf_event_fd_array_release(struct bpf_map *map,
struct bpf_event_entry *ee; struct bpf_event_entry *ee;
int i; int i;
if (map->map_flags & BPF_F_PRESERVE_ELEMS)
return;
rcu_read_lock(); rcu_read_lock();
for (i = 0; i < array->map.max_entries; i++) { for (i = 0; i < array->map.max_entries; i++) {
ee = READ_ONCE(array->ptrs[i]); ee = READ_ONCE(array->ptrs[i]);
...@@ -1143,12 +1151,19 @@ static void perf_event_fd_array_release(struct bpf_map *map, ...@@ -1143,12 +1151,19 @@ static void perf_event_fd_array_release(struct bpf_map *map,
rcu_read_unlock(); rcu_read_unlock();
} }
static void perf_event_fd_array_map_free(struct bpf_map *map)
{
if (map->map_flags & BPF_F_PRESERVE_ELEMS)
bpf_fd_array_map_clear(map);
fd_array_map_free(map);
}
static int perf_event_array_map_btf_id; static int perf_event_array_map_btf_id;
const struct bpf_map_ops perf_event_array_map_ops = { const struct bpf_map_ops perf_event_array_map_ops = {
.map_meta_equal = bpf_map_meta_equal, .map_meta_equal = bpf_map_meta_equal,
.map_alloc_check = fd_array_map_alloc_check, .map_alloc_check = fd_array_map_alloc_check,
.map_alloc = array_map_alloc, .map_alloc = array_map_alloc,
.map_free = fd_array_map_free, .map_free = perf_event_fd_array_map_free,
.map_get_next_key = array_map_get_next_key, .map_get_next_key = array_map_get_next_key,
.map_lookup_elem = fd_array_map_lookup_elem, .map_lookup_elem = fd_array_map_lookup_elem,
.map_delete_elem = fd_array_map_delete_elem, .map_delete_elem = fd_array_map_delete_elem,
......
...@@ -414,6 +414,9 @@ enum { ...@@ -414,6 +414,9 @@ enum {
/* Enable memory-mapping BPF map */ /* Enable memory-mapping BPF map */
BPF_F_MMAPABLE = (1U << 10), BPF_F_MMAPABLE = (1U << 10),
/* Share perf_event among processes */
BPF_F_PRESERVE_ELEMS = (1U << 11),
}; };
/* Flags for BPF_PROG_QUERY. */ /* Flags for BPF_PROG_QUERY. */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment