Skip to content

Commit c78420b

Browse files
anakryikoMartin KaFai Lau
authored andcommitted
libbpf: improve early detection of doomed-to-fail BPF program loading
Extend libbpf's pre-load checks for BPF programs, detecting more typical conditions that are destinated to cause BPF program failure. This is an opportunity to provide more helpful and actionable error message to users, instead of potentially very confusing BPF verifier log and/or error. In this case, we detect struct_ops BPF program that was not referenced anywhere, but still attempted to be loaded (according to libbpf logic). Suggest that the program might need to be used in some struct_ops variable. User will get a message of the following kind: libbpf: prog 'test_1_forgotten': SEC("struct_ops") program isn't referenced anywhere, did you forget to use it? Suggested-by: Tejun Heo <tj@kernel.org> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/r/20240507001335.1445325-6-andrii@kernel.org Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
1 parent 548c2ed commit c78420b

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

tools/lib/bpf/libbpf.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7372,14 +7372,27 @@ static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog
73727372
__u32 log_level = prog->log_level;
73737373
int ret, err;
73747374

7375-
if (prog->type == BPF_PROG_TYPE_UNSPEC) {
7375+
/* Be more helpful by rejecting programs that can't be validated early
7376+
* with more meaningful and actionable error message.
7377+
*/
7378+
switch (prog->type) {
7379+
case BPF_PROG_TYPE_UNSPEC:
73767380
/*
73777381
* The program type must be set. Most likely we couldn't find a proper
73787382
* section definition at load time, and thus we didn't infer the type.
73797383
*/
73807384
pr_warn("prog '%s': missing BPF prog type, check ELF section name '%s'\n",
73817385
prog->name, prog->sec_name);
73827386
return -EINVAL;
7387+
case BPF_PROG_TYPE_STRUCT_OPS:
7388+
if (prog->attach_btf_id == 0) {
7389+
pr_warn("prog '%s': SEC(\"struct_ops\") program isn't referenced anywhere, did you forget to use it?\n",
7390+
prog->name);
7391+
return -EINVAL;
7392+
}
7393+
break;
7394+
default:
7395+
break;
73837396
}
73847397

73857398
if (!insns || !insns_cnt)

0 commit comments

Comments
 (0)