libbpf: Fix BTF handling in bpf_program__clone()
Subsystems (4)
PATCH 1/2 libbpf: Fix BTF handling in bpf_program__clone()
From: Mykyta Yatsenko <[email protected]>
Align bpf_program__clone() with bpf_object_load_prog() by gating BTF func/line info on FEAT_BTF_FUNC kernel support, and resolve caller-provided prog_btf_fd before checking obj->btf so that callers with their own BTF can use clone() even when the object has no BTF loaded.
While at it, treat func_info and line_info fields as atomic groups to prevent mismatches between pointer and count from different sources.
Move bpf_program__clone() to libbpf 1.8.
--- tools/lib/bpf/libbpf.c | 66 ++++++++++++++++++++++++++-------------- tools/lib/bpf/libbpf.map | 2 +- 2 files changed, 45 insertions(+), 23 deletions(-)
tools/lib/bpf/libbpf.c +44 -22
@@ -9852,7 +9852,7 @@ int bpf_program__clone(struct bpf_program *prog, const struct bpf_prog_load_opts {LIBBPF_OPTS (bpf_prog_load_opts, attr);struct bpf_object *obj; -int err, fd; +int err, fd, prog_btf_fd;if (!prog)return libbpf_err (-EINVAL );
@@ -9865,8 +9865,9 @@ int bpf_program__clone(struct bpf_program *prog, const struct bpf_prog_load_optsreturn libbpf_err (-EINVAL );/* -* Caller-provided opts take priority; fall back to -* prog/object defaults when the caller leaves them zero. +* Caller-provided opts take priority; fall back to prog/object defaults when the +* caller leaves them zero. 0/NULL means "use default". Callers cannot explicitly +* clear an inherited non-zero value by passing 0. */ attr.attach_prog_fd =OPTS_GET (opts, attach_prog_fd,0 ) ?: prog->attach_prog_fd; attr.prog_flags =OPTS_GET (opts, prog_flags,0 ) ?: prog->prog_flags;
@@ -9878,27 +9879,45 @@ int bpf_program__clone(struct bpf_program *prog, const struct bpf_prog_load_optsif (attr.token_fd) attr.prog_flags |=BPF_F_TOKEN_FD ; -/* BTF func/line info */ -if (obj->btf &&btf__fd (obj->btf) >=0 ) { - attr.prog_btf_fd =OPTS_GET (opts, prog_btf_fd,0 ) ?:btf__fd (obj->btf); - attr.func_info =OPTS_GET (opts, func_info,NULL ) ?: prog->func_info; - attr.func_info_cnt =OPTS_GET (opts, func_info_cnt,0 ) ?: prog->func_info_cnt; - attr.func_info_rec_size = -OPTS_GET (opts, func_info_rec_size,0 ) ?: prog->func_info_rec_size; - attr.line_info =OPTS_GET (opts, line_info,NULL ) ?: prog->line_info; - attr.line_info_cnt =OPTS_GET (opts, line_info_cnt,0 ) ?: prog->line_info_cnt; - attr.line_info_rec_size = -OPTS_GET (opts, line_info_rec_size,0 ) ?: prog->line_info_rec_size; + prog_btf_fd =OPTS_GET (opts, prog_btf_fd,0 ); +if (!prog_btf_fd && obj->btf) + prog_btf_fd =btf__fd (obj->btf); + +/* BTF func/line info: only pass if kernel supports it */ +if (kernel_supports (obj,FEAT_BTF_FUNC ) && prog_btf_fd >0 ) { + attr.prog_btf_fd = prog_btf_fd; + +/* Treat func_info fields as an atomic group */ +if (OPTS_GET (opts, func_info,NULL )) { + attr.func_info =OPTS_GET (opts, func_info,NULL ); + attr.func_info_cnt =OPTS_GET (opts, func_info_cnt,0 ); + attr.func_info_rec_size =OPTS_GET (opts, func_info_rec_size,0 ); + }else { + attr.func_info = prog->func_info; + attr.func_info_cnt = prog->func_info_cnt; + attr.func_info_rec_size = prog->func_info_rec_size; + } + +/* Treat line_info fields as an atomic group */ +if (OPTS_GET (opts, line_info,NULL )) { + attr.line_info =OPTS_GET (opts, line_info,NULL ); + attr.line_info_cnt =OPTS_GET (opts, line_info_cnt,0 ); + attr.line_info_rec_size =OPTS_GET (opts, line_info_rec_size,0 ); + }else { + attr.line_info = prog->line_info; + attr.line_info_cnt = prog->line_info_cnt; + attr.line_info_rec_size = prog->line_info_rec_size; + } } +/* Logging is caller-controlled; no fallback to prog/obj log settings */ attr.log_buf =OPTS_GET (opts, log_buf,NULL ); attr.log_size =OPTS_GET (opts, log_size,0 ); attr.log_level =OPTS_GET (opts, log_level,0 );/* -* Fields below may be mutated by prog_prepare_load_fn: -* Seed them from prog/obj defaults here; -* Later override with caller-provided opts. +* Fields below may be mutated by prog_prepare_load_fn(). Seed them from prog/obj +* defaults here; re-apply caller overrides afterwards */ attr.expected_attach_type = prog->expected_attach_type; attr.attach_btf_id = prog->attach_btf_id;
@@ -9910,16 +9929,19 @@ int bpf_program__clone(struct bpf_program *prog, const struct bpf_prog_load_optsreturn libbpf_err (err); } -/* Re-apply caller overrides for output fields */ +/* Re-apply caller overrides for fields that prog_prepare_load_fn may have modified */ if (OPTS_GET (opts, expected_attach_type,0 )) - attr.expected_attach_type = -OPTS_GET (opts, expected_attach_type,0 ); + attr.expected_attach_type =OPTS_GET (opts, expected_attach_type,0 );if (OPTS_GET (opts, attach_btf_id,0 )) attr.attach_btf_id =OPTS_GET (opts, attach_btf_id,0 );if (OPTS_GET (opts, attach_btf_obj_fd,0 )) - attr.attach_btf_obj_fd = -OPTS_GET (opts, attach_btf_obj_fd,0 ); + attr.attach_btf_obj_fd =OPTS_GET (opts, attach_btf_obj_fd,0 ); +/* +* Unlike bpf_object_load_prog(), we intentionally do not call bpf_prog_bind_map() +* for RODATA maps here to avoid mutating the object's state. Callers can bind the +* required maps themselves using bpf_prog_bind_map(). +*/ fd =bpf_prog_load (prog->type, prog->name, obj->license, prog->insns, prog->insns_cnt, &attr);
tools/lib/bpf/libbpf.map +1 -1
@@ -452,12 +452,12 @@ LIBBPF_1.7.0 { bpf_map__set_exclusive_program; bpf_map__exclusive_program; bpf_prog_assoc_struct_ops; - bpf_program__clone; bpf_program__assoc_struct_ops; btf__permute; } LIBBPF_1.6.0; LIBBPF_1.8.0 { global: + bpf_program__clone; btf__new_empty_opts; } LIBBPF_1.7.0;
Signature · 1 line
2.52.0
PATCH 2/2 selftests/bpf: Suppress veristat error messages in non-verbose mode
From: Mykyta Yatsenko <[email protected]>
When running veristat across many BPF objects, expected load failures produce noisy stderr output that obscures actual issues. Gate these diagnostic messages behind --verbose.
--- tools/testing/selftests/bpf/veristat.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/veristat.c +3 -2
@@ -1673,7 +1673,8 @@ static int process_prog(const char *filename, struct bpf_object *obj, struct bpf fd =bpf_program__clone (prog, &opts);if (fd <0 ) { err = fd; -fprintf (stderr,"Failed to load program %s %d\n" , prog_name, err); +if (env.verbose) +fprintf (stderr,"Failed to load program %s %d\n" , prog_name, err); } mem_peak_b =cgroup_memory_peak ();if (!cgroup_err && mem_peak_a >=0 && mem_peak_b >=0 )
@@ -2247,7 +2248,7 @@ static int process_obj(const char *filename) } err =bpf_object__prepare (obj); -if (err)/* run process_prog() anyway to output per program failures */ +if (err && env.verbose)/* run process_prog() anyway to output per program failures */ fprintf (stderr,"Failed to prepare BPF object for loading %d\n" , err);bpf_object__for_each_program (prog, obj) {
Signature · 1 line
2.52.0
Andrii Nakryiko
AppliedRe: [PATCH bpf-next 2/2] selftests/bpf: Suppress veristat error messages in non-verbose mode
Quoted 12 lines · Mykyta Yatsenko
From: Mykyta Yatsenko <[email protected]>
When running veristat across many BPF objects, expected load failures produce noisy stderr output that obscures actual issues. Gate these diagnostic messages behind --verbose.
Signed-off-by Mykyta Yatsenko <[email protected]>--- tools/testing/selftests/bpf/veristat.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
applied this individual patch to bpf-next
Quoted 26 lines
tools/testing/selftests/bpf/veristat.c +3
-3@@ -1673,7 +1673,8 @@ static int process_prog(const char *filename, struct bpf_object *obj, struct bpf fd =bpf_program__clone (prog, &opts);if (fd <0 ) { err = fd; -fprintf (stderr,"Failed to load program %s %d\n" , prog_name, err); +if (env.verbose) +fprintf (stderr,"Failed to load program %s %d\n" , prog_name, err); } mem_peak_b =cgroup_memory_peak ();if (!cgroup_err && mem_peak_a >=0 && mem_peak_b >=0 )@@ -2247,7 +2248,7 @@ static int process_obj(const char *filename) } err =bpf_object__prepare (obj); -if (err)/* run process_prog() anyway to output per program failures */ +if (err && env.verbose)/* run process_prog() anyway to output per program failures */ fprintf (stderr,"Failed to prepare BPF object for loading %d\n" , err);bpf_object__for_each_program (prog, obj) { --2.52.0
[email protected]
AI reviewThe commit message says this aligns with bpf_object_load_prog(), but that function checks btf__fd(obj->btf) >= 0:
Quoted 3 lines
tools/lib/bpf/libbpf.c +0
-0
[ ... ]
tools/lib/bpf/libbpf.c int bpf_program__clone(struct bpf_program *prog, const struct bpf_prog_load_opts · line 9887
Quoted 9 lines
tools/lib/bpf/libbpf.c +6
-3-/* BTF func/line info */ -if (obj->btf &&btf__fd (obj->btf) >=0 ) { - attr.prog_btf_fd =OPTS_GET (opts, prog_btf_fd,0 ) ?:btf__fd (obj->btf); + prog_btf_fd =OPTS_GET (opts, prog_btf_fd,0 ); +if (!prog_btf_fd && obj->btf) + prog_btf_fd =btf__fd (obj->btf); + +/* BTF func/line info: only pass if kernel supports it */ +if (kernel_supports (obj,FEAT_BTF_FUNC ) && prog_btf_fd >0 ) {
The commit message says this aligns with bpf_object_load_prog(), but that function checks btf__fd(obj->btf) >= 0:
if (obj->btf && btf__fd(obj->btf) >= 0 && kernel_supports(obj, FEAT_BTF_FUNC)) {Should the check here be prog_btf_fd >= 0 rather than prog_btf_fd > 0, to match the reference implementation?
[ ... ]
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/23811027187
Quoted 19 lines · On 3/31/26 7:35 PM, [email protected] wrote:
Quoted 3 lines
tools/lib/bpf/libbpf.c +0
-0[ ... ]
tools/lib/bpf/libbpf.c int bpf_program__clone(struct bpf_program *prog, const struct bpf_prog_load_opts · line 9887
Quoted 9 lines
tools/lib/bpf/libbpf.c +6
-3-/* BTF func/line info */ -if (obj->btf &&btf__fd (obj->btf) >=0 ) { - attr.prog_btf_fd =OPTS_GET (opts, prog_btf_fd,0 ) ?:btf__fd (obj->btf); + prog_btf_fd =OPTS_GET (opts, prog_btf_fd,0 ); +if (!prog_btf_fd && obj->btf) + prog_btf_fd =btf__fd (obj->btf); + +/* BTF func/line info: only pass if kernel supports it */ +if (kernel_supports (obj,FEAT_BTF_FUNC ) && prog_btf_fd >0 ) {The commit message says this aligns with
bpf_object_load_prog(), but that function checksbtf__fd(obj->btf)>= 0:
Quoted 4 lines
if (obj->btf && btf__fd(obj->btf) >= 0 && kernel_supports(obj, FEAT_BTF_FUNC)) {Should the check here be
prog_btf_fd>= 0 rather thanprog_btf_fd> 0, to match the reference implementation?
this is intentional because 0 is a default value for the prog_btf_fd, that the field takes when it's unset, so to detect that situation, I'm filtering out fd = 0, even though it is a valid fd.
Quoted 9 lines
[ ... ]
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.mdCI run summary: https://github.com/kernel-patches/bpf/actions/runs/23811027187
Quoted 61 lines · Mykyta Yatsenko
From: Mykyta Yatsenko <[email protected]>
Align
bpf_program__clone()withbpf_object_load_prog()by gating BTF func/line info onFEAT_BTF_FUNCkernel support, and resolve caller-providedprog_btf_fdbefore checkingobj->btfso that callers with their own BTF can useclone()even when the object has no BTF loaded.While at it, treat
func_infoandline_infofields as atomic groups to prevent mismatches between pointer and count from different sources.Move
bpf_program__clone()to libbpf 1.8.Fixes 970bd2dced35 ("libbpf: Introduce bpf_program__clone()")Signed-off-by Mykyta Yatsenko <[email protected]>--- tools/lib/bpf/libbpf.c | 66 ++++++++++++++++++++++++++-------------- tools/lib/bpf/libbpf.map | 2 +- 2 files changed, 45 insertions(+), 23 deletions(-)tools/lib/bpf/libbpf.c +4
-14@@ -9852,7 +9852,7 @@ int bpf_program__clone(struct bpf_program *prog, const struct bpf_prog_load_opts {LIBBPF_OPTS (bpf_prog_load_opts, attr);struct bpf_object *obj; -int err, fd; +int err, fd, prog_btf_fd;if (!prog)return libbpf_err (-EINVAL );@@ -9865,8 +9865,9 @@ int bpf_program__clone(struct bpf_program *prog, const struct bpf_prog_load_optsreturn libbpf_err (-EINVAL );/* -* Caller-provided opts take priority; fall back to -* prog/object defaults when the caller leaves them zero. +* Caller-provided opts take priority; fall back to prog/object defaults when the +* caller leaves them zero. 0/NULL means "use default". Callers cannot explicitly +* clear an inherited non-zero value by passing 0. */ attr.attach_prog_fd =OPTS_GET (opts, attach_prog_fd,0 ) ?: prog->attach_prog_fd; attr.prog_flags =OPTS_GET (opts, prog_flags,0 ) ?: prog->prog_flags;@@ -9878,27 +9879,45 @@ int bpf_program__clone(struct bpf_program *prog, const struct bpf_prog_load_optsif (attr.token_fd) attr.prog_flags |=BPF_F_TOKEN_FD ; -/* BTF func/line info */ -if (obj->btf &&btf__fd (obj->btf) >=0 ) { - attr.prog_btf_fd =OPTS_GET (opts, prog_btf_fd,0 ) ?:btf__fd (obj->btf); - attr.func_info =OPTS_GET (opts, func_info,NULL ) ?: prog->func_info; - attr.func_info_cnt =OPTS_GET (opts, func_info_cnt,0 ) ?: prog->func_info_cnt; - attr.func_info_rec_size = -OPTS_GET (opts, func_info_rec_size,0 ) ?: prog->func_info_rec_size; - attr.line_info =OPTS_GET (opts, line_info,NULL ) ?: prog->line_info; - attr.line_info_cnt =OPTS_GET (opts, line_info_cnt,0 ) ?: prog->line_info_cnt; - attr.line_info_rec_size = -OPTS_GET (opts, line_info_rec_size,0 ) ?: prog->line_info_rec_size;
tools/lib/bpf/libbpf.c int bpf_program__clone(struct bpf_program *prog, const struct bpf_prog_load_opts · line 9891
Quoted 10 lines
tools/lib/bpf/libbpf.c +10
-0+ prog_btf_fd =OPTS_GET (opts, prog_btf_fd,0 ); +if (!prog_btf_fd && obj->btf) + prog_btf_fd =btf__fd (obj->btf); + +/* BTF func/line info: only pass if kernel supports it */ +if (kernel_supports (obj,FEAT_BTF_FUNC ) && prog_btf_fd >0 ) { + attr.prog_btf_fd = prog_btf_fd; + +/* Treat func_info fields as an atomic group */ +if (OPTS_GET (opts, func_info,NULL )) {
let's actually validate that either all three related fields are specified or not, and error out otherwise
same for line_info group
pw-bot: cr
Quoted 81 lines
tools/lib/bpf/libbpf.c +30
-8+ attr.func_info =OPTS_GET (opts, func_info,NULL ); + attr.func_info_cnt =OPTS_GET (opts, func_info_cnt,0 ); + attr.func_info_rec_size =OPTS_GET (opts, func_info_rec_size,0 ); + }else { + attr.func_info = prog->func_info; + attr.func_info_cnt = prog->func_info_cnt; + attr.func_info_rec_size = prog->func_info_rec_size; + } + +/* Treat line_info fields as an atomic group */ +if (OPTS_GET (opts, line_info,NULL )) { + attr.line_info =OPTS_GET (opts, line_info,NULL ); + attr.line_info_cnt =OPTS_GET (opts, line_info_cnt,0 ); + attr.line_info_rec_size =OPTS_GET (opts, line_info_rec_size,0 ); + }else { + attr.line_info = prog->line_info; + attr.line_info_cnt = prog->line_info_cnt; + attr.line_info_rec_size = prog->line_info_rec_size; + } } +/* Logging is caller-controlled; no fallback to prog/obj log settings */ attr.log_buf =OPTS_GET (opts, log_buf,NULL ); attr.log_size =OPTS_GET (opts, log_size,0 ); attr.log_level =OPTS_GET (opts, log_level,0 );/* -* Fields below may be mutated by prog_prepare_load_fn: -* Seed them from prog/obj defaults here; -* Later override with caller-provided opts. +* Fields below may be mutated by prog_prepare_load_fn(). Seed them from prog/obj +* defaults here; re-apply caller overrides afterwards */ attr.expected_attach_type = prog->expected_attach_type; attr.attach_btf_id = prog->attach_btf_id;@@ -9910,16 +9929,19 @@ int bpf_program__clone(struct bpf_program *prog, const struct bpf_prog_load_optsreturn libbpf_err (err); } -/* Re-apply caller overrides for output fields */ +/* Re-apply caller overrides for fields that prog_prepare_load_fn may have modified */ if (OPTS_GET (opts, expected_attach_type,0 )) - attr.expected_attach_type = -OPTS_GET (opts, expected_attach_type,0 ); + attr.expected_attach_type =OPTS_GET (opts, expected_attach_type,0 );if (OPTS_GET (opts, attach_btf_id,0 )) attr.attach_btf_id =OPTS_GET (opts, attach_btf_id,0 );if (OPTS_GET (opts, attach_btf_obj_fd,0 )) - attr.attach_btf_obj_fd = -OPTS_GET (opts, attach_btf_obj_fd,0 ); + attr.attach_btf_obj_fd =OPTS_GET (opts, attach_btf_obj_fd,0 ); +/* +* Unlike bpf_object_load_prog(), we intentionally do not call bpf_prog_bind_map() +* for RODATA maps here to avoid mutating the object's state. Callers can bind the +* required maps themselves using bpf_prog_bind_map(). +*/ fd =bpf_prog_load (prog->type, prog->name, obj->license, prog->insns, prog->insns_cnt, &attr);tools/lib/bpf/libbpf.map +1
-2@@ -452,12 +452,12 @@ LIBBPF_1.7.0 { bpf_map__set_exclusive_program; bpf_map__exclusive_program; bpf_prog_assoc_struct_ops; - bpf_program__clone; bpf_program__assoc_struct_ops; btf__permute; } LIBBPF_1.6.0; LIBBPF_1.8.0 { global: + bpf_program__clone; btf__new_empty_opts; } LIBBPF_1.7.0; --2.52.0