Skip to content

Commit 4c91c59

Browse files
mykyta5Alexei Starovoitov
authored andcommitted
bpf: improve error message for unsupported helper
BPF verifier emits "unknown func" message when given BPF program type does not support BPF helper. This message may be confusing for users, as important context that helper is unknown only to current program type is not provided. This patch changes message to "program of this type cannot use helper " and aligns dependent code in libbpf and tests. Any suggestions on improving/changing this message are welcome. Signed-off-by: Mykyta Yatsenko <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Acked-by: Quentin Monnet <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 2a720cc commit 4c91c59

File tree

5 files changed

+13
-10
lines changed

5 files changed

+13
-10
lines changed

kernel/bpf/verifier.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10188,8 +10188,8 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
1018810188
if (env->ops->get_func_proto)
1018910189
fn = env->ops->get_func_proto(func_id, env->prog);
1019010190
if (!fn) {
10191-
verbose(env, "unknown func %s#%d\n", func_id_name(func_id),
10192-
func_id);
10191+
verbose(env, "program of this type cannot use helper %s#%d\n",
10192+
func_id_name(func_id), func_id);
1019310193
return -EINVAL;
1019410194
}
1019510195

tools/bpf/bpftool/feature.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,8 @@ probe_helper_ifindex(enum bpf_func_id id, enum bpf_prog_type prog_type,
664664

665665
probe_prog_load_ifindex(prog_type, insns, ARRAY_SIZE(insns), buf,
666666
sizeof(buf), ifindex);
667-
res = !grep(buf, "invalid func ") && !grep(buf, "unknown func ");
667+
res = !grep(buf, "invalid func ") && !grep(buf, "unknown func ") &&
668+
!grep(buf, "program of this type cannot use helper ");
668669

669670
switch (get_vendor_id(ifindex)) {
670671
case 0x19ee: /* Netronome specific */

tools/lib/bpf/libbpf_probes.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,8 @@ int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type, enum bpf_func_id helpe
448448
/* If BPF verifier doesn't recognize BPF helper ID (enum bpf_func_id)
449449
* at all, it will emit something like "invalid func unknown#181".
450450
* If BPF verifier recognizes BPF helper but it's not supported for
451-
* given BPF program type, it will emit "unknown func bpf_sys_bpf#166".
451+
* given BPF program type, it will emit "unknown func bpf_sys_bpf#166"
452+
* or "program of this type cannot use helper bpf_sys_bpf#166".
452453
* In both cases, provided combination of BPF program type and BPF
453454
* helper is not supported by the kernel.
454455
* In all other cases, probe_prog_load() above will either succeed (e.g.,
@@ -457,7 +458,8 @@ int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type, enum bpf_func_id helpe
457458
* that), or we'll get some more specific BPF verifier error about
458459
* some unsatisfied conditions.
459460
*/
460-
if (ret == 0 && (strstr(buf, "invalid func ") || strstr(buf, "unknown func ")))
461+
if (ret == 0 && (strstr(buf, "invalid func ") || strstr(buf, "unknown func ") ||
462+
strstr(buf, "program of this type cannot use helper ")))
461463
return 0;
462464
return 1; /* assume supported */
463465
}

tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ static void test_rel_setsockopt(void)
304304
struct bpf_dctcp_release *rel_skel;
305305
libbpf_print_fn_t old_print_fn;
306306

307-
err_str = "unknown func bpf_setsockopt";
307+
err_str = "program of this type cannot use helper bpf_setsockopt";
308308
found = false;
309309

310310
old_print_fn = libbpf_set_print(libbpf_debug_print);

tools/testing/selftests/bpf/progs/verifier_helper_restricted.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct {
3030

3131
SEC("kprobe")
3232
__description("bpf_ktime_get_coarse_ns is forbidden in BPF_PROG_TYPE_KPROBE")
33-
__failure __msg("unknown func bpf_ktime_get_coarse_ns")
33+
__failure __msg("program of this type cannot use helper bpf_ktime_get_coarse_ns")
3434
__naked void in_bpf_prog_type_kprobe_1(void)
3535
{
3636
asm volatile (" \
@@ -44,7 +44,7 @@ __naked void in_bpf_prog_type_kprobe_1(void)
4444

4545
SEC("tracepoint")
4646
__description("bpf_ktime_get_coarse_ns is forbidden in BPF_PROG_TYPE_TRACEPOINT")
47-
__failure __msg("unknown func bpf_ktime_get_coarse_ns")
47+
__failure __msg("program of this type cannot use helper bpf_ktime_get_coarse_ns")
4848
__naked void in_bpf_prog_type_tracepoint_1(void)
4949
{
5050
asm volatile (" \
@@ -58,7 +58,7 @@ __naked void in_bpf_prog_type_tracepoint_1(void)
5858

5959
SEC("perf_event")
6060
__description("bpf_ktime_get_coarse_ns is forbidden in BPF_PROG_TYPE_PERF_EVENT")
61-
__failure __msg("unknown func bpf_ktime_get_coarse_ns")
61+
__failure __msg("program of this type cannot use helper bpf_ktime_get_coarse_ns")
6262
__naked void bpf_prog_type_perf_event_1(void)
6363
{
6464
asm volatile (" \
@@ -72,7 +72,7 @@ __naked void bpf_prog_type_perf_event_1(void)
7272

7373
SEC("raw_tracepoint")
7474
__description("bpf_ktime_get_coarse_ns is forbidden in BPF_PROG_TYPE_RAW_TRACEPOINT")
75-
__failure __msg("unknown func bpf_ktime_get_coarse_ns")
75+
__failure __msg("program of this type cannot use helper bpf_ktime_get_coarse_ns")
7676
__naked void bpf_prog_type_raw_tracepoint_1(void)
7777
{
7878
asm volatile (" \

0 commit comments

Comments
 (0)