Skip to content

Commit 792cacc

Browse files
liu-song-6Alexei Starovoitov
authored andcommitted
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: Song Liu <songliubraving@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20200930224927.1936644-2-songliubraving@fb.com
1 parent 3effc06 commit 792cacc

3 files changed

Lines changed: 23 additions & 2 deletions

File tree

‎include/uapi/linux/bpf.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,9 @@ enum {
414414

415415
/* Enable memory-mapping BPF map */
416416
BPF_F_MMAPABLE = (1U << 10),
417+
418+
/* Share perf_event among processes */
419+
BPF_F_PRESERVE_ELEMS = (1U << 11),
417420
};
418421

419422
/* Flags for BPF_PROG_QUERY. */

‎kernel/bpf/arraymap.c‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
#include "map_in_map.h"
1616

1717
#define ARRAY_CREATE_FLAG_MASK \
18-
(BPF_F_NUMA_NODE | BPF_F_MMAPABLE | BPF_F_ACCESS_MASK)
18+
(BPF_F_NUMA_NODE | BPF_F_MMAPABLE | BPF_F_ACCESS_MASK | \
19+
BPF_F_PRESERVE_ELEMS)
1920

2021
static void bpf_array_free_percpu(struct bpf_array *array)
2122
{
@@ -64,6 +65,10 @@ int array_map_alloc_check(union bpf_attr *attr)
6465
attr->map_flags & BPF_F_MMAPABLE)
6566
return -EINVAL;
6667

68+
if (attr->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY &&
69+
attr->map_flags & BPF_F_PRESERVE_ELEMS)
70+
return -EINVAL;
71+
6772
if (attr->value_size > KMALLOC_MAX_SIZE)
6873
/* if value_size is bigger, the user space won't be able to
6974
* access the elements.
@@ -1134,6 +1139,9 @@ static void perf_event_fd_array_release(struct bpf_map *map,
11341139
struct bpf_event_entry *ee;
11351140
int i;
11361141

1142+
if (map->map_flags & BPF_F_PRESERVE_ELEMS)
1143+
return;
1144+
11371145
rcu_read_lock();
11381146
for (i = 0; i < array->map.max_entries; i++) {
11391147
ee = READ_ONCE(array->ptrs[i]);
@@ -1143,12 +1151,19 @@ static void perf_event_fd_array_release(struct bpf_map *map,
11431151
rcu_read_unlock();
11441152
}
11451153

1154+
static void perf_event_fd_array_map_free(struct bpf_map *map)
1155+
{
1156+
if (map->map_flags & BPF_F_PRESERVE_ELEMS)
1157+
bpf_fd_array_map_clear(map);
1158+
fd_array_map_free(map);
1159+
}
1160+
11461161
static int perf_event_array_map_btf_id;
11471162
const struct bpf_map_ops perf_event_array_map_ops = {
11481163
.map_meta_equal = bpf_map_meta_equal,
11491164
.map_alloc_check = fd_array_map_alloc_check,
11501165
.map_alloc = array_map_alloc,
1151-
.map_free = fd_array_map_free,
1166+
.map_free = perf_event_fd_array_map_free,
11521167
.map_get_next_key = array_map_get_next_key,
11531168
.map_lookup_elem = fd_array_map_lookup_elem,
11541169
.map_delete_elem = fd_array_map_delete_elem,

‎tools/include/uapi/linux/bpf.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,9 @@ enum {
414414

415415
/* Enable memory-mapping BPF map */
416416
BPF_F_MMAPABLE = (1U << 10),
417+
418+
/* Share perf_event among processes */
419+
BPF_F_PRESERVE_ELEMS = (1U << 11),
417420
};
418421

419422
/* Flags for BPF_PROG_QUERY. */

0 commit comments

Comments
 (0)