libbpf: Fix perm errors for LDIMM_64_FULL_RANGE_OFF use
Emil Tsalapatis
reviewercover libbpf: Fix perm errors for LDIMM_64_FULL_RANGE_OFF use
Commit 728ff167910e ("libbpf: Add gating for arena globals relocation feature") adds a feature flag for testing whether the running kernel supports LDIMM64 instructions with large direct offsets. Fix two edge cases that can cause unexpected -EPERM errors in two ways:
1) The probe program used for the feature has type TRACEPOINT, but it's possible the caller does not have permission to load it, even if it is able to do so for generic BPF programs. Use the SOCKET_FILTER type instead that requires fewer permissions. This does not affect the check itself, which will always fail verification anyway.
2) The probe is triggered during bpf_object__collect_relos(), itself called in bpf_object_open(), to compute the arena relocation offsets of arena variables. However, the caller may not have permissions to load BPF programs. This is the case in some systems with the bpftool calls made by the BPF selftests during compilation, e.g., for skeleton generation. Move all uses of the feature check to bpf_object_prepare() time instead.
v1 -> v2: https://lore.kernel.org/bpf/[email protected]/
- Adjust the hash of the original commit post-tree rebase
- Ensure close() is not called on invalid prog_fd in feature probe (Coverity)
Emil Tsalapatis (2):
libbpf: Do not use PROG_TYPE_TRACEPOINT program for feature gating
libbpf: Delay feature gate check until object prepare time
tools/lib/bpf/features.c | 6 +++--- tools/lib/bpf/libbpf.c | 31 +++++++++++++++++++++++-------- 2 files changed, 26 insertions(+), 11 deletions(-)
Signature · 1 line
2.49.0
Emil Tsalapatis
reviewerPATCH v2 1/2 libbpf: Do not use PROG_TYPE_TRACEPOINT program for feature gating
Commit 728ff167910e uses a PROG_TYPE_TRACEPOINT BPF test program to check whether the running kernel supports large LDIMM64 offsets. The feature gate incorrectly assumes that the program will fail at verification time with one of two messages, depending on whether the feature is supported by the running kernel. However, PROG_TYPE_TRACEPOINT programs may fail to load before verification even starts, e.g., if the shell does not have the appropriate capabilities. Use a BPF_PROG_TYPE_SOCKET_FILTER program for the feature gate instead.
Also fix two minor issues. First, ensure the log buffer for the test is
initialized: Failing program load before verification led to libbpf dumping uninitialized data to stdout. Also, ensure that close() is only called for program_fd in the probe if the program load actually succeeded. The call was currently failing silently with -EBADF most of the time.
--- tools/lib/bpf/features.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
tools/lib/bpf/features.c +3 -3
@@ -508,7 +508,7 @@ static int probe_kern_arg_ctx_tag(int token_fd)static int probe_ldimm64_full_range_off (int token_fd) { -char log_buf[1024 ]; +char log_buf[1024 ] = {};int prog_fd, map_fd;int ret; LIBBPF_OPTS(bpf_map_create_opts, map_opts,
@@ -536,14 +536,14 @@ static int probe_ldimm64_full_range_off(int token_fd) } insns[0 ].imm = map_fd; - prog_fd =bpf_prog_load (BPF_PROG_TYPE_TRACEPOINT ,"global_reloc" ,"GPL" , insns, insn_cnt, &prog_opts); + prog_fd =bpf_prog_load (BPF_PROG_TYPE_SOCKET_FILTER ,"global_reloc" ,"GPL" , insns, insn_cnt, &prog_opts); ret = -errno;close (map_fd); -close (prog_fd);if (prog_fd >=0 ) {pr_warn ("Error in %s(): Program loading unexpectedly succeeded.\n" , __func__); +close (prog_fd);return -EINVAL ; }
Signature · 1 line
2.49.0
Re: [PATCH bpf v2 1/2] libbpf: Do not use PROG_TYPE_TRACEPOINT program for feature gating
Quoted 28 lines · Emil Tsalapatis
Commit
728ff167910euses aPROG_TYPE_TRACEPOINTBPF test program to check whether the running kernel supports large LDIMM64 offsets. The feature gate incorrectly assumes that the program will fail at verification time with one of two messages, depending on whether the feature is supported by the running kernel. However,PROG_TYPE_TRACEPOINTprograms may fail to load before verification even starts, e.g., if the shell does not have the appropriate capabilities. Use aBPF_PROG_TYPE_SOCKET_FILTERprogram for the feature gate instead.Also fix two minor issues. First, ensure the log buffer for the test is
initialized: Failing program load before verification led to libbpf dumping uninitialized data to stdout. Also, ensure thatclose()is only called forprogram_fdin the probe if the program load actually succeeded. The call was currently failing silently with-EBADFmost of the time.Reported-by Alexei Starovoitov <[email protected]>Fixes 728ff167910e ("libbpf: Add gating for arena globals relocation feature")Signed-off-by Emil Tsalapatis <[email protected]>--- tools/lib/bpf/features.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)tools/lib/bpf/features.c +0
-0
tools/lib/bpf/features.c static int probe_kern_arg_ctx_tag(int token_fd) · line 511
Quoted 5 lines
tools/lib/bpf/features.c +1
-1@@ -508,7 +508,7 @@ static int probe_kern_arg_ctx_tag(int token_fd)static int probe_ldimm64_full_range_off (int token_fd) { -char log_buf[1024 ]; +char log_buf[1024 ] = {};
minor nit: this will clear whole log_buf, but we really just need to set first character to zero, so instead I'd do
log_buf[0] = '\0';
before program load
minor, because this is not performance critical code, but it's a bit wasteful nevertheless
Quoted 24 lines
tools/lib/bpf/features.c +2
-3int prog_fd, map_fd;int ret; LIBBPF_OPTS(bpf_map_create_opts, map_opts,@@ -536,14 +536,14 @@ static int probe_ldimm64_full_range_off(int token_fd) } insns[0 ].imm = map_fd; - prog_fd =bpf_prog_load (BPF_PROG_TYPE_TRACEPOINT ,"global_reloc" ,"GPL" , insns, insn_cnt, &prog_opts); + prog_fd =bpf_prog_load (BPF_PROG_TYPE_SOCKET_FILTER ,"global_reloc" ,"GPL" , insns, insn_cnt, &prog_opts); ret = -errno;close (map_fd); -close (prog_fd);if (prog_fd >=0 ) {pr_warn ("Error in %s(): Program loading unexpectedly succeeded.\n" , __func__); +close (prog_fd);return -EINVAL ; } --2.49.0
Emil Tsalapatis
reviewerPATCH v2 2/2 libbpf: Delay feature gate check until object prepare time
In reply to Emil TsalapatisCommit 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.
--- tools/lib/bpf/libbpf.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-)
tools/lib/bpf/libbpf.c +23 -8
@@ -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;
Signature · 1 line
2.49.0
Re: [PATCH bpf v2 2/2] libbpf: Delay feature gate check until object prepare time
Quoted 13 lines · Emil Tsalapatis
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 duringbpf_object__collect_relos, itself called bybpf_object_open.However, the program calling
bpf_object_openmay 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.
Quoted 9 lines
Avoid this by moving all uses of the
FEAT_LDIMM64_FULL_RANGE_OFFfeature gate to BPF object preparation time instead.Fixes 728ff167910e ("libbpf: Add gating for arena globals relocation feature")Signed-off-by Emil Tsalapatis <[email protected]>--- tools/lib/bpf/libbpf.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-)
This works, but see some suggestions for a follow up to contain feature-checking a bit better
Quoted 8 lines
tools/lib/bpf/libbpf.c +0
-0@@ -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;
tools/lib/bpf/libbpf.c static int init_arena_map_data(struct bpf_object *obj, struct bpf_map *map, · line 3016
Quoted 10 lines
tools/lib/bpf/libbpf.c +5
-5-/* 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;
I'd keep this obj->arena_data_off at zero at this point (we don't use it until prepare step, skeleton also just uses obj->arena_data directly without offset during open phase)...
tools/lib/bpf/libbpf.c static int bpf_program__record_reloc(struct bpf_program *prog, · line 4675
Quoted 9 lines
tools/lib/bpf/libbpf.c +1
-1/* 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;
this is good, relos don't need to record this offset...
Quoted 13 lines
tools/lib/bpf/libbpf.c +1
-0map = &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;
tools/lib/bpf/libbpf.c bpf_object__create_maps(struct bpf_object *obj) · line 5643
Quoted 10 lines
tools/lib/bpf/libbpf.c +7
-1}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;
and here I'd instead set arena_data_off to its final value (zero or proper offset), something like
if (kernel_supports(obj, FEAT_LDIMM64_FULL_RANGE_OFF)
obj->arena_data_off = bpf_map_mmap_sz(map) -
roundup(sysconf(_SC_PAGE_SIZE), sysconf(_SC_PAGE_SIZE));
(note, we did all the correctness checks early in init_arena_map_data, we don't have to do any more here)
and then the rest of the code stays exactly the same, down to the original memcpy(map->mmaped + obj->arena_data_off, ...), and no extra variables
Quoted 6 lines
tools/lib/bpf/libbpf.c +2
-0+ +memcpy (arena_globals, obj->arena_data, obj->arena_data_sz);zfree (&obj->arena_data); }
tools/lib/bpf/libbpf.c bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog) · line 6403
Quoted 9 lines
tools/lib/bpf/libbpf.c +6
-0@@ -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; + } +
here too we don't need feature checking, our obj->arena_data_off should have a valid value (might be zero, that's fine)
tools/lib/bpf/libbpf.c int bpf_object__load_skeleton(struct bpf_object_skeleton *s) · line 14475
Quoted 10 lines
tools/lib/bpf/libbpf.c +1
-1if (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;
and here as well, no need to expose feature check here, we have map->obj->arena_data_off set correctly
Quoted 5 lines
tools/lib/bpf/libbpf.c +0
-1else *map_skel->mmaped = map->mmaped; --2.49.0
Emil Tsalapatis
reviewerRe: [PATCH bpf v2 2/2] libbpf: Delay feature gate check until object prepare time
Quoted 17 lines · Andrii Nakryiko
Quoted 16 lines · Emil Tsalapatis
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 duringbpf_object__collect_relos, itself called bybpf_object_open.However, the program calling
bpf_object_openmay 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_OFFfeature gate to BPF object preparation time instead.
Quoted 6 lines
Quoted 6 lines
Fixes 728ff167910e ("libbpf: Add gating for arena globals relocation feature")Signed-off-by Emil Tsalapatis <[email protected]>--- tools/lib/bpf/libbpf.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-)
Hi Andrii,
ack to this and the point for the first patch, two minor followups below.
Quoted 12 lines
This works, but see some suggestions for a follow up to contain feature-checking a bit better
Quoted 8 lines
tools/lib/bpf/libbpf.c +0
-0@@ -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;
Quoted 15 lines
tools/lib/bpf/libbpf.c static int init_arena_map_data(struct bpf_object *obj, struct bpf_map *map, · line 3016
Quoted 10 lines
tools/lib/bpf/libbpf.c +5
-5-/* 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;I'd keep this
obj->arena_data_offat zero at this point (we don't use it until prepare step, skeleton also just usesobj->arena_datadirectly without offset during open phase)...
Do we even want to keep the assignment? The field should be 0 by default since we're calloc()'ing the object.
Quoted 39 lines
tools/lib/bpf/libbpf.c static int bpf_program__record_reloc(struct bpf_program *prog, · line 4675
Quoted 9 lines
tools/lib/bpf/libbpf.c +1
-1/* 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;this is good, relos don't need to record this offset...
Quoted 13 lines
tools/lib/bpf/libbpf.c +1
-0map = &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;tools/lib/bpf/libbpf.c bpf_object__create_maps(struct bpf_object *obj) · line 5643
Quoted 10 lines
tools/lib/bpf/libbpf.c +7
-1}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;and here I'd instead set
arena_data_offto its final value (zero or proper offset), something like
Quoted 4 lines
if (
kernel_supports(obj, FEAT_LDIMM64_FULL_RANGE_OFF)obj->arena_data_off = bpf_map_mmap_sz(map) -
roundup(sysconf(_SC_PAGE_SIZE), sysconf(_SC_PAGE_SIZE));
(I assume roundup is roundup(arena->arena_data_sz, sysconf(_SC_PAGE_SIZE))
I assume the trivial duplication of recomputing the rounded up value here and in init_arena_map_data is fine - rounding up data_sz to the nearest page is verbose but easy to understand imo.
Quoted 46 lines
(note, we did all the correctness checks early in
init_arena_map_data, we don't have to do any more here)and then the rest of the code stays exactly the same, down to the original
memcpy(map->mmaped + obj->arena_data_off, ...), and no extra variablesQuoted 6 lines
tools/lib/bpf/libbpf.c +2
-0+ +memcpy (arena_globals, obj->arena_data, obj->arena_data_sz);zfree (&obj->arena_data); }tools/lib/bpf/libbpf.c bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog) · line 6403
Quoted 9 lines
tools/lib/bpf/libbpf.c +6
-0@@ -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; + } +here too we don't need feature checking, our
obj->arena_data_offshould have a valid value (might be zero, that's fine)tools/lib/bpf/libbpf.c int bpf_object__load_skeleton(struct bpf_object_skeleton *s) · line 14475
Quoted 10 lines
tools/lib/bpf/libbpf.c +1
-1if (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;and here as well, no need to expose feature check here, we have
map->obj->arena_data_offset correctlyQuoted 5 lines
tools/lib/bpf/libbpf.c +0
-1else *map_skel->mmaped = map->mmaped; --2.49.0
Emil Tsalapatis
reviewerRe: [PATCH bpf v2 2/2] libbpf: Delay feature gate check until object prepare time
Quoted 104 lines · Emil Tsalapatis
Quoted 17 lines · Andrii Nakryiko
Quoted 16 lines · Emil Tsalapatis
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 duringbpf_object__collect_relos, itself called bybpf_object_open.However, the program calling
bpf_object_openmay 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_OFFfeature gate to BPF object preparation time instead.Quoted 6 lines
Quoted 6 lines
Fixes 728ff167910e ("libbpf: Add gating for arena globals relocation feature")Signed-off-by Emil Tsalapatis <[email protected]>--- tools/lib/bpf/libbpf.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-)Hi Andrii,
ack to this and the point for the first patch, two minor followups below.
Quoted 12 lines
This works, but see some suggestions for a follow up to contain feature-checking a bit better
Quoted 8 lines
tools/lib/bpf/libbpf.c +0
-0@@ -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;Quoted 15 lines
tools/lib/bpf/libbpf.c static int init_arena_map_data(struct bpf_object *obj, struct bpf_map *map, · line 3016
Quoted 10 lines
tools/lib/bpf/libbpf.c +5
-5-/* 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;I'd keep this
obj->arena_data_offat zero at this point (we don't use it until prepare step, skeleton also just usesobj->arena_datadirectly without offset during open phase)...Do we even want to keep the assignment? The field should be 0 by default since we're calloc()'ing the object.
Quoted 39 lines
tools/lib/bpf/libbpf.c static int bpf_program__record_reloc(struct bpf_program *prog, · line 4675
Quoted 9 lines
tools/lib/bpf/libbpf.c +1
-1/* 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;this is good, relos don't need to record this offset...
Quoted 13 lines
tools/lib/bpf/libbpf.c +1
-0map = &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;tools/lib/bpf/libbpf.c bpf_object__create_maps(struct bpf_object *obj) · line 5643
Quoted 10 lines
tools/lib/bpf/libbpf.c +7
-1}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;and here I'd instead set
arena_data_offto its final value (zero or proper offset), something likeQuoted 4 lines
if (
kernel_supports(obj, FEAT_LDIMM64_FULL_RANGE_OFF)obj->arena_data_off = bpf_map_mmap_sz(map) -
roundup(sysconf(_SC_PAGE_SIZE), sysconf(_SC_PAGE_SIZE));
Quoted 7 lines
(I assume roundup is
roundup(arena->arena_data_sz, sysconf(_SC_PAGE_SIZE))I assume the trivial duplication of recomputing the rounded up value here and in
init_arena_map_datais fine - rounding updata_szto the nearest page is verbose but easy to understand imo.
Actually there is an issue: If we move the flag check here, then we haven't properly calculated arena_data_off by the time we do the relocations in bpf_object__relocate_data(). I'll just put the single feature in bpf_object__realocate instead.
Quoted 46 lines
Quoted 46 lines
(note, we did all the correctness checks early in
init_arena_map_data, we don't have to do any more here)and then the rest of the code stays exactly the same, down to the original
memcpy(map->mmaped + obj->arena_data_off, ...), and no extra variablesQuoted 6 lines
tools/lib/bpf/libbpf.c +2
-0+ +memcpy (arena_globals, obj->arena_data, obj->arena_data_sz);zfree (&obj->arena_data); }tools/lib/bpf/libbpf.c bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog) · line 6403
Quoted 9 lines
tools/lib/bpf/libbpf.c +6
-0@@ -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; + } +here too we don't need feature checking, our
obj->arena_data_offshould have a valid value (might be zero, that's fine)tools/lib/bpf/libbpf.c int bpf_object__load_skeleton(struct bpf_object_skeleton *s) · line 14475
Quoted 10 lines
tools/lib/bpf/libbpf.c +1
-1if (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;and here as well, no need to expose feature check here, we have
map->obj->arena_data_offset correctlyQuoted 5 lines
tools/lib/bpf/libbpf.c +0
-1else *map_skel->mmaped = map->mmaped; --2.49.0
This series was applied to bpf/bpf.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf.git (master)
by Alexei Starovoitov <[email protected]>:
Quoted 5 lines · On Fri, 13 Feb 2026 21:10:11 -0500 you wrote:
Commit
728ff167910e("libbpf: Add gating for arena globals relocation feature") adds a feature flag for testing whether the running kernel supports LDIMM64 instructions with large direct offsets. Fix two edge cases that can cause unexpected-EPERMerrors in two ways:
Quoted 7 lines
1) The probe program used for the feature has type TRACEPOINT, but it's possible the caller does not have permission to load it, even if it is able to do so for generic BPF programs. Use the
SOCKET_FILTERtype instead that requires fewer permissions. This does not affect the check itself, which will always fail verification anyway.[...]
Here is the summary with links:
- [bpf,v2,1/2] libbpf: Do not use PROG_TYPE_TRACEPOINT program for feature gating
https://git.kernel.org/bpf/bpf/c/623e7b898a0b - [bpf,v2,2/2] libbpf: Delay feature gate check until object prepare time https://git.kernel.org/bpf/bpf/c/9e07e5f57d93
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html