Skip to contentSkip to conversation

bpf: Allow void return type for global subprogs

PATCH Superseded Emil Tsalapatis 1 participant 3 messages Feb 10

Subsystems (4)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Emil Tsalapatis

author reviewer
#0 lore raw

v2 cover bpf: Allow void return type for global subprogs

Global subprogs are currently not allowed to return void. Adjust verifier logic to allow global functions with a void return type. Exception callbacks are excluded from this change, and still require a scalar return type.

Also adjust existing selftests that were ensuring that global subprogs with void return types fail to verify. These tests should now load successfully.

v1 -> v2
(https://lore.kernel.org/bpf/[email protected]/)

- Only mark R0 as valid for non-void global functions (AI)
- Rename subprog_is_void to subprog_returns_void (Kumar)
- Add test that R0 is invalid after calling a void global (Alexei)
- Add check to ensure exception callbacks do not return void and associated selftest

Cc Kumar Kartikeya Dwivedi <[email protected]>

Emil Tsalapatis (2):
bpf: Allow void global functions in the verifier
selftests: Add selftests for void globals

 kernel/bpf/btf.c                              |  7 +--
 kernel/bpf/verifier.c                         | 49 ++++++++++++++++---
 .../bpf/prog_tests/test_global_funcs.c        |  2 +
 .../selftests/bpf/progs/exceptions_fail.c     | 19 +++++--
 .../selftests/bpf/progs/test_global_func18.c  | 23 +++++++++
 .../selftests/bpf/progs/test_global_func7.c   |  2 +-
 6 files changed, 89 insertions(+), 13 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/test_global_func18.c
Signature · 1 line
2.49.0
author reviewer
#1 lore raw

PATCH v2 1/2 bpf: Allow void global functions in the verifier

Global subprogs are currently not allowed to return void. Adjust verifier logic to allow global functions with a void return type.

---
 kernel/bpf/btf.c                              |  7 +--
 kernel/bpf/verifier.c                         | 49 ++++++++++++++++---
 .../selftests/bpf/progs/exceptions_fail.c     |  2 +-
 .../selftests/bpf/progs/test_global_func7.c   |  2 +-
 4 files changed, 49 insertions(+), 11 deletions(-)
kernel/bpf/btf.c +4 -3
index 7708958e3fb8..51f8d3dece5d 100644
@@ -7836,15 +7836,16 @@ int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog)
 			tname, nargs, MAX_BPF_FUNC_REG_ARGS);
 		return -EINVAL;
 	}
-	/* check that function returns int, exception cb also requires this */
+	/* check that function is void or returns int, exception cb also requires this */
 	t = btf_type_by_id(btf, t->type);
 	while (btf_type_is_modifier(t))
 		t = btf_type_by_id(btf, t->type);
-	if (!btf_type_is_int(t) && !btf_is_any_enum(t)) {
+	if (!btf_type_is_void(t) && !btf_type_is_int(t) && !btf_is_any_enum(t)) {
 		if (!is_global)
 			return -EINVAL;
 		bpf_log(log,
-			"Global function %s() doesn't return scalar. Only those are supported.\n",
+			"Global function %s() return value not void or scalar. "
+			"Only those are supported.\n",
 			tname);
 		return -EINVAL;
 	}
kernel/bpf/verifier.c +43 -6

74 lines not on this page · raw message

tools/testing/selftests/bpf/progs/exceptions_fail.c +1 -1
index 8a0fdff89927..d28ecc4ee2d0 100644
@@ -51,7 +51,7 @@ __noinline int exception_cb_ok_arg_small(int a)
 
 SEC("?tc")
 __exception_cb(exception_cb_bad_ret_type)
-__failure __msg("Global function exception_cb_bad_ret_type() doesn't return scalar.")
+__failure __msg("Global function exception_cb_bad_ret_type() return value not void or scalar.")
 int reject_exception_cb_type_1(struct __sk_buff *ctx)
 {
 	bpf_throw(0);
tools/testing/selftests/bpf/progs/test_global_func7.c +1 -1
index f182febfde3c..9e59625c1c92 100644
@@ -12,7 +12,7 @@ void foo(struct __sk_buff *skb)
 }
 
 SEC("tc")
-__failure __msg("foo() doesn't return scalar")
+__success
 int global_func7(struct __sk_buff *skb)
 {
 	foo(skb);
Signature · 1 line
2.49.0
author reviewer
#2 lore raw

PATCH v2 2/2 selftests: Add selftests for void globals

In reply to Emil Tsalapatis

Add additional testing for void global functions. The tests ensure that calls to void global functions properly keep R0 invalid. Also make sure that exception callbacks still require a return value.

---
 .../bpf/prog_tests/test_global_funcs.c        |  2 ++
 .../selftests/bpf/progs/exceptions_fail.c     | 19 ++++++++++++---
 .../selftests/bpf/progs/test_global_func18.c  | 23 +++++++++++++++++++
 3 files changed, 41 insertions(+), 3 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/test_global_func18.c
tools/testing/selftests/bpf/prog_tests/test_global_funcs.c +2 -0
index e905cbaf6b3d..45854cbcbfcf 100644
@@ -18,6 +18,7 @@
 #include "test_global_func15.skel.h"
 #include "test_global_func16.skel.h"
 #include "test_global_func17.skel.h"
+#include "test_global_func18.skel.h"
 #include "test_global_func_ctx_args.skel.h"
 
 #include "bpf/libbpf_internal.h"
@@ -155,6 +156,7 @@ void test_test_global_funcs(void)
 	RUN_TESTS(test_global_func15);
 	RUN_TESTS(test_global_func16);
 	RUN_TESTS(test_global_func17);
+	RUN_TESTS(test_global_func18);
 	RUN_TESTS(test_global_func_ctx_args);
 
 	if (test__start_subtest("ctx_arg_rewrite"))
tools/testing/selftests/bpf/progs/exceptions_fail.c +16 -3
index d28ecc4ee2d0..691c16001952 100644
@@ -29,11 +29,15 @@ struct {
 private(A) struct bpf_spin_lock lock;
 private(A) struct bpf_rb_root rbtree __contains(foo, node);
 
-__noinline void *exception_cb_bad_ret_type(u64 cookie)
+__noinline void *exception_cb_bad_ret_type1(u64 cookie)
 {
 	return NULL;
 }
 
+__noinline void exception_cb_bad_ret_type2(u64 cookie)
+{
+}
+
 __noinline int exception_cb_bad_arg_0(void)
 {
 	return 0;
@@ -50,8 +54,8 @@ __noinline int exception_cb_ok_arg_small(int a)
 }
 
 SEC("?tc")
-__exception_cb(exception_cb_bad_ret_type)
-__failure __msg("Global function exception_cb_bad_ret_type() return value not void or scalar.")
+__exception_cb(exception_cb_bad_ret_type1)
+__failure __msg("Global function exception_cb_bad_ret_type1() return value not void or scalar.")
 int reject_exception_cb_type_1(struct __sk_buff *ctx)
 {
 	bpf_throw(0);
@@ -85,6 +89,15 @@ int reject_exception_cb_type_4(struct __sk_buff *ctx)
 	return 0;
 }
 
+SEC("?tc")
+__exception_cb(exception_cb_bad_ret_type2)
+__failure __msg("exception cb cannot return void")
+int reject_exception_cb_type_5(struct __sk_buff *ctx)
+{
+	bpf_throw(0);
+	return 0;
+}
+
 __noinline
 static int timer_cb(void *map, int *key, struct bpf_timer *timer)
 {
tools/testing/selftests/bpf/progs/test_global_func18.c new file +23 -0
new file mode 100644
index 000000000000..3dafb0dc2342
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+
+__weak
+void foo(void)
+{
+}
+
+SEC("tc")
+__failure __msg("!read_ok")
+int global_func18(struct __sk_buff *skb)
+{
+	foo();
+
+	asm volatile(
+		"r1 = r0;"
+		:::
+	);
+
+	return 0;
+}
Signature · 1 line
2.49.0