Skip to contentSkip to conversation

bpf: improve error message for unsupported helper

PATCH Applied Mykyta Yatsenko [email protected] 4 participants 4 messages Mar 25, 2024 – Mar 27, 2024

Subsystems (6)
Kselftest BPF BPF selftests BPF core +2 more subsystems
lore
Share & tools
lore

Mykyta Yatsenko [email protected]

author
#0 lore raw

PATCH 1/1 bpf: improve error message for unsupported helper

From: Mykyta Yatsenko <[email protected]>

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.

---
 kernel/bpf/verifier.c                                     | 4 ++--
 tools/bpf/bpftool/feature.c                               | 3 ++-
 tools/lib/bpf/libbpf_probes.c                             | 6 ++++--
 tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c       | 2 +-
 .../selftests/bpf/progs/verifier_helper_restricted.c      | 8 ++++----
 5 files changed, 13 insertions(+), 10 deletions(-)
kernel/bpf/verifier.c +2 -2
index ca6cacf7b42f..a1a96ba867d9 100644
@@ -10188,8 +10188,8 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
 	if (env->ops->get_func_proto)
 		fn = env->ops->get_func_proto(func_id, env->prog);
 	if (!fn) {
-		verbose(env, "unknown func %s#%d\n", func_id_name(func_id),
-			func_id);
+		verbose(env, "program of this type cannot use helper %s#%d\n",
+			func_id_name(func_id), func_id);
 		return -EINVAL;
 	}
 
tools/bpf/bpftool/feature.c +2 -1
index 708733b0ea06..c754a428c8c6 100644
@@ -664,7 +664,8 @@ probe_helper_ifindex(enum bpf_func_id id, enum bpf_prog_type prog_type,
 
 	probe_prog_load_ifindex(prog_type, insns, ARRAY_SIZE(insns), buf,
 				sizeof(buf), ifindex);
-	res = !grep(buf, "invalid func ") && !grep(buf, "unknown func ");
+	res = !grep(buf, "invalid func ") && !grep(buf, "unknown func ") &&
+		!grep(buf, "program of this type cannot use helper ");
 
 	switch (get_vendor_id(ifindex)) {
 	case 0x19ee: /* Netronome specific */
tools/lib/bpf/libbpf_probes.c +4 -2
index 302188122439..feef28040cdb 100644
@@ -448,7 +448,8 @@ int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type, enum bpf_func_id helpe
 	/* If BPF verifier doesn't recognize BPF helper ID (enum bpf_func_id)
 	 * at all, it will emit something like "invalid func unknown#181".
 	 * If BPF verifier recognizes BPF helper but it's not supported for
-	 * given BPF program type, it will emit "unknown func bpf_sys_bpf#166".
+	 * given BPF program type, it will emit "unknown func bpf_sys_bpf#166"
+	 * or "program of this type cannot use helper bpf_sys_bpf#166".
 	 * In both cases, provided combination of BPF program type and BPF
 	 * helper is not supported by the kernel.
 	 * 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
 	 * that), or we'll get some more specific BPF verifier error about
 	 * some unsatisfied conditions.
 	 */
-	if (ret == 0 && (strstr(buf, "invalid func ") || strstr(buf, "unknown func ")))
+	if (ret == 0 && (strstr(buf, "invalid func ") || strstr(buf, "unknown func ") ||
+			 strstr(buf, "program of this type cannot use helper ")))
 		return 0;
 	return 1; /* assume supported */
 }
tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c +1 -1
index a88e6e07e4f5..c646c812a3bf 100644
@@ -315,7 +315,7 @@ static void test_rel_setsockopt(void)
 	struct bpf_dctcp_release *rel_skel;
 	libbpf_print_fn_t old_print_fn;
 
-	err_str = "unknown func bpf_setsockopt";
+	err_str = "program of this type cannot use helper bpf_setsockopt";
 	found = false;
 
 	old_print_fn = libbpf_set_print(libbpf_debug_print);
tools/testing/selftests/bpf/progs/verifier_helper_restricted.c +4 -4
index 0ede0ccd090c..059aa716e3d0 100644
@@ -30,7 +30,7 @@ struct {
 
 SEC("kprobe")
 __description("bpf_ktime_get_coarse_ns is forbidden in BPF_PROG_TYPE_KPROBE")
-__failure __msg("unknown func bpf_ktime_get_coarse_ns")
+__failure __msg("program of this type cannot use helper bpf_ktime_get_coarse_ns")
 __naked void in_bpf_prog_type_kprobe_1(void)
 {
 	asm volatile ("					\
@@ -44,7 +44,7 @@ __naked void in_bpf_prog_type_kprobe_1(void)
 
 SEC("tracepoint")
 __description("bpf_ktime_get_coarse_ns is forbidden in BPF_PROG_TYPE_TRACEPOINT")
-__failure __msg("unknown func bpf_ktime_get_coarse_ns")
+__failure __msg("program of this type cannot use helper bpf_ktime_get_coarse_ns")
 __naked void in_bpf_prog_type_tracepoint_1(void)
 {
 	asm volatile ("					\
@@ -58,7 +58,7 @@ __naked void in_bpf_prog_type_tracepoint_1(void)
 
 SEC("perf_event")
 __description("bpf_ktime_get_coarse_ns is forbidden in BPF_PROG_TYPE_PERF_EVENT")
-__failure __msg("unknown func bpf_ktime_get_coarse_ns")
+__failure __msg("program of this type cannot use helper bpf_ktime_get_coarse_ns")
 __naked void bpf_prog_type_perf_event_1(void)
 {
 	asm volatile ("					\
@@ -72,7 +72,7 @@ __naked void bpf_prog_type_perf_event_1(void)
 
 SEC("raw_tracepoint")
 __description("bpf_ktime_get_coarse_ns is forbidden in BPF_PROG_TYPE_RAW_TRACEPOINT")
-__failure __msg("unknown func bpf_ktime_get_coarse_ns")
+__failure __msg("program of this type cannot use helper bpf_ktime_get_coarse_ns")
 __naked void bpf_prog_type_raw_tracepoint_1(void)
 {
 	asm volatile ("					\
Signature · 1 line
2.42.0
Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

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.

Quoted 6 lines

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.

---

"unknown func bpf_somehelper" is one of the common confusions users run into (and I had to explain multiple times), so this more meaningful message is definitely an improvement, thanks!

Quoted 7 lines
 kernel/bpf/verifier.c                                     | 4 ++--
 tools/bpf/bpftool/feature.c                               | 3 ++-
 tools/lib/bpf/libbpf_probes.c                             | 6 ++++--
 tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c       | 2 +-
 .../selftests/bpf/progs/verifier_helper_restricted.c      | 8 ++++----
 5 files changed, 13 insertions(+), 10 deletions(-)

[...]

maintainer Acked-by
#2 lore raw
Quoted 14 lines · Andrii Nakryiko
Quoted 12 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

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.

Quoted 8 lines
Quoted 2 lines

---

"unknown func bpf_somehelper" is one of the common confusions users run into (and I had to explain multiple times), so this more meaningful message is definitely an improvement, thanks!

Same here; and thanks for including the bpftool update!

#3 lore raw
In reply to Mykyta Yatsenko [email protected]
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 8 lines · On Mon, 25 Mar 2024 15:22:10 +0000 you wrote:

From: Mykyta Yatsenko <[email protected]>

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.

[...]

Here is the summary with links:
- [bpf-next] bpf: improve error message for unsupported helper

    https://git.kernel.org/bpf/bpf-next/c/4c91c5925ac1

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html