Skip to content

Commit 79ff13e

Browse files
Alexei Starovoitovanakryiko
authored andcommitted
libbpf: Add support for bpf_arena.
mmap() bpf_arena right after creation, since the kernel needs to remember the address returned from mmap. This is user_vm_start. LLVM will generate bpf_arena_cast_user() instructions where necessary and JIT will add upper 32-bit of user_vm_start to such pointers. Fix up bpf_map_mmap_sz() to compute mmap size as map->value_size * map->max_entries for arrays and PAGE_SIZE * map->max_entries for arena. Don't set BTF at arena creation time, since it doesn't support it. Signed-off-by: Alexei Starovoitov <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 4d2b560 commit 79ff13e

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ static const char * const map_type_name[] = {
185185
[BPF_MAP_TYPE_BLOOM_FILTER] = "bloom_filter",
186186
[BPF_MAP_TYPE_USER_RINGBUF] = "user_ringbuf",
187187
[BPF_MAP_TYPE_CGRP_STORAGE] = "cgrp_storage",
188+
[BPF_MAP_TYPE_ARENA] = "arena",
188189
};
189190

190191
static const char * const prog_type_name[] = {
@@ -1684,7 +1685,7 @@ static struct bpf_map *bpf_object__add_map(struct bpf_object *obj)
16841685
return map;
16851686
}
16861687

1687-
static size_t bpf_map_mmap_sz(unsigned int value_sz, unsigned int max_entries)
1688+
static size_t array_map_mmap_sz(unsigned int value_sz, unsigned int max_entries)
16881689
{
16891690
const long page_sz = sysconf(_SC_PAGE_SIZE);
16901691
size_t map_sz;
@@ -1694,6 +1695,20 @@ static size_t bpf_map_mmap_sz(unsigned int value_sz, unsigned int max_entries)
16941695
return map_sz;
16951696
}
16961697

1698+
static size_t bpf_map_mmap_sz(const struct bpf_map *map)
1699+
{
1700+
const long page_sz = sysconf(_SC_PAGE_SIZE);
1701+
1702+
switch (map->def.type) {
1703+
case BPF_MAP_TYPE_ARRAY:
1704+
return array_map_mmap_sz(map->def.value_size, map->def.max_entries);
1705+
case BPF_MAP_TYPE_ARENA:
1706+
return page_sz * map->def.max_entries;
1707+
default:
1708+
return 0; /* not supported */
1709+
}
1710+
}
1711+
16971712
static int bpf_map_mmap_resize(struct bpf_map *map, size_t old_sz, size_t new_sz)
16981713
{
16991714
void *mmaped;
@@ -1847,7 +1862,7 @@ bpf_object__init_internal_map(struct bpf_object *obj, enum libbpf_map_type type,
18471862
pr_debug("map '%s' (global data): at sec_idx %d, offset %zu, flags %x.\n",
18481863
map->name, map->sec_idx, map->sec_offset, def->map_flags);
18491864

1850-
mmap_sz = bpf_map_mmap_sz(map->def.value_size, map->def.max_entries);
1865+
mmap_sz = bpf_map_mmap_sz(map);
18511866
map->mmaped = mmap(NULL, mmap_sz, PROT_READ | PROT_WRITE,
18521867
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
18531868
if (map->mmaped == MAP_FAILED) {
@@ -5017,6 +5032,7 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, b
50175032
case BPF_MAP_TYPE_SOCKHASH:
50185033
case BPF_MAP_TYPE_QUEUE:
50195034
case BPF_MAP_TYPE_STACK:
5035+
case BPF_MAP_TYPE_ARENA:
50205036
create_attr.btf_fd = 0;
50215037
create_attr.btf_key_type_id = 0;
50225038
create_attr.btf_value_type_id = 0;
@@ -5261,7 +5277,19 @@ bpf_object__create_maps(struct bpf_object *obj)
52615277
if (err < 0)
52625278
goto err_out;
52635279
}
5264-
5280+
if (map->def.type == BPF_MAP_TYPE_ARENA) {
5281+
map->mmaped = mmap((void *)map->map_extra, bpf_map_mmap_sz(map),
5282+
PROT_READ | PROT_WRITE,
5283+
map->map_extra ? MAP_SHARED | MAP_FIXED : MAP_SHARED,
5284+
map->fd, 0);
5285+
if (map->mmaped == MAP_FAILED) {
5286+
err = -errno;
5287+
map->mmaped = NULL;
5288+
pr_warn("map '%s': failed to mmap arena: %d\n",
5289+
map->name, err);
5290+
return err;
5291+
}
5292+
}
52655293
if (map->init_slots_sz && map->def.type != BPF_MAP_TYPE_PROG_ARRAY) {
52665294
err = init_map_in_map_slots(obj, map);
52675295
if (err < 0)
@@ -8761,7 +8789,7 @@ static void bpf_map__destroy(struct bpf_map *map)
87618789
if (map->mmaped) {
87628790
size_t mmap_sz;
87638791

8764-
mmap_sz = bpf_map_mmap_sz(map->def.value_size, map->def.max_entries);
8792+
mmap_sz = bpf_map_mmap_sz(map);
87658793
munmap(map->mmaped, mmap_sz);
87668794
map->mmaped = NULL;
87678795
}
@@ -9995,11 +10023,14 @@ int bpf_map__set_value_size(struct bpf_map *map, __u32 size)
999510023
return libbpf_err(-EBUSY);
999610024

999710025
if (map->mmaped) {
9998-
int err;
999910026
size_t mmap_old_sz, mmap_new_sz;
10027+
int err;
10028+
10029+
if (map->def.type != BPF_MAP_TYPE_ARRAY)
10030+
return -EOPNOTSUPP;
1000010031

10001-
mmap_old_sz = bpf_map_mmap_sz(map->def.value_size, map->def.max_entries);
10002-
mmap_new_sz = bpf_map_mmap_sz(size, map->def.max_entries);
10032+
mmap_old_sz = bpf_map_mmap_sz(map);
10033+
mmap_new_sz = array_map_mmap_sz(size, map->def.max_entries);
1000310034
err = bpf_map_mmap_resize(map, mmap_old_sz, mmap_new_sz);
1000410035
if (err) {
1000510036
pr_warn("map '%s': failed to resize memory-mapped region: %d\n",
@@ -13530,7 +13561,7 @@ int bpf_object__load_skeleton(struct bpf_object_skeleton *s)
1353013561

1353113562
for (i = 0; i < s->map_cnt; i++) {
1353213563
struct bpf_map *map = *s->maps[i].map;
13533-
size_t mmap_sz = bpf_map_mmap_sz(map->def.value_size, map->def.max_entries);
13564+
size_t mmap_sz = bpf_map_mmap_sz(map);
1353413565
int prot, map_fd = map->fd;
1353513566
void **mmaped = s->maps[i].mmaped;
1353613567

tools/lib/bpf/libbpf_probes.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,13 @@ static int probe_map_create(enum bpf_map_type map_type)
338338
key_size = 0;
339339
max_entries = 1;
340340
break;
341+
case BPF_MAP_TYPE_ARENA:
342+
key_size = 0;
343+
value_size = 0;
344+
max_entries = 1; /* one page */
345+
opts.map_extra = 0; /* can mmap() at any address */
346+
opts.map_flags = BPF_F_MMAPABLE;
347+
break;
341348
case BPF_MAP_TYPE_HASH:
342349
case BPF_MAP_TYPE_ARRAY:
343350
case BPF_MAP_TYPE_PROG_ARRAY:

0 commit comments

Comments
 (0)