Commit 728ff167910e ("libbpf: Add gating for arena globals relocation feature") adds a feature gate check that loads a map and BPF program to test the running kernel supports large direct offsets for LDIMM64 instructions. This check is currently used to calculate arena symbol offsets during bpf_object__collect_relos, itself called by bpf_object_open. However, the program calling bpf_object_open may not have the permissions to load maps and programs. This is the case with the BPF selftests, where bpftool is invoked at compilation time during skeleton generation. This causes errors as the feature gate unexpectedly fails with -EPERM. Avoid this by moving all uses of the FEAT_LDIMM64_FULL_RANGE_OFF feature gate to BPF object preparation time instead. Fixes: 728ff167910e ("libbpf: Add gating for arena globals relocation feature") Signed-off-by: Emil Tsalapatis --- tools/lib/bpf/libbpf.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 93e59ed8d9a1..ff38a9db1b13 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -3009,11 +3009,11 @@ static int init_arena_map_data(struct bpf_object *obj, struct bpf_map *map, memcpy(obj->arena_data, data, data_sz); obj->arena_data_sz = data_sz; - /* place globals at the end of the arena (if supported) */ - if (kernel_supports(obj, FEAT_LDIMM64_FULL_RANGE_OFF)) - obj->arena_data_off = mmap_sz - data_alloc_sz; - else - obj->arena_data_off = 0; + /* + * possible offset for arena globals into the arena, + * if supported at load time + */ + obj->arena_data_off = mmap_sz - data_alloc_sz; /* make bpf_map__init_value() work for ARENA maps */ map->mmaped = obj->arena_data; @@ -4672,7 +4672,7 @@ static int bpf_program__record_reloc(struct bpf_program *prog, reloc_desc->type = RELO_DATA; reloc_desc->insn_idx = insn_idx; reloc_desc->map_idx = obj->arena_map_idx; - reloc_desc->sym_off = sym->st_value + obj->arena_data_off; + reloc_desc->sym_off = sym->st_value; map = &obj->maps[obj->arena_map_idx]; pr_debug("prog '%s': found arena map %d (%s, sec %d, off %zu) for insn %u\n", @@ -5556,6 +5556,7 @@ bpf_object__create_maps(struct bpf_object *obj) { struct bpf_map *map; unsigned int i, j; + char *arena_globals; int err; bool retried; @@ -5633,7 +5634,15 @@ bpf_object__create_maps(struct bpf_object *obj) return err; } if (obj->arena_data) { - memcpy(map->mmaped + obj->arena_data_off, obj->arena_data, + /* + * Only relocate the globals to the end of the arena + * if the kernel supports it. + */ + arena_globals = map->mmaped; + if (kernel_supports(obj, FEAT_LDIMM64_FULL_RANGE_OFF)) + arena_globals += obj->arena_data_off; + + memcpy(arena_globals, obj->arena_data, obj->arena_data_sz); zfree(&obj->arena_data); } @@ -6386,6 +6395,12 @@ bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog) case RELO_DATA: map = &obj->maps[relo->map_idx]; insn[1].imm = insn[0].imm + relo->sym_off; + + if (relo->map_idx == obj->arena_map_idx && + kernel_supports(obj, FEAT_LDIMM64_FULL_RANGE_OFF)) { + insn[1].imm += obj->arena_data_off; + } + if (obj->gen_loader) { insn[0].src_reg = BPF_PSEUDO_MAP_IDX_VALUE; insn[0].imm = relo->map_idx; @@ -14456,7 +14471,7 @@ int bpf_object__load_skeleton(struct bpf_object_skeleton *s) if (!map_skel->mmaped) continue; - if (map->def.type == BPF_MAP_TYPE_ARENA) + if (map->def.type == BPF_MAP_TYPE_ARENA && kernel_supports(*s->obj, FEAT_LDIMM64_FULL_RANGE_OFF)) *map_skel->mmaped = map->mmaped + map->obj->arena_data_off; else *map_skel->mmaped = map->mmaped; -- 2.49.0