allow calling kfuncs in normal tracepoint programs
Subsystems (3)
cover allow calling kfuncs in normal tracepoint programs
It is possible to call a kfunc within a raw tp_btf program but not possible within a normal tracepoint program. Currently, it seems the verifier receives -EACCESS from fetch_kfunc_meta() as a result of not finding any hook within btf_kfunc_id_set_contains().
The first patch exposes the issue (as a selftest) while the second patch updates the kfunc hook lookup to account for tracepoint programs.
Pre-submission CI run: https://github.com/kernel-patches/bpf/pull/7539
JP Kobryn (2):
bpf/selftests: coverage for calling kfuncs within tracepoint
bpf: allow kfuncs within normal tracepoint programs
kernel/bpf/btf.c | 1 + .../selftests/bpf/prog_tests/kfunc_in_tp.c | 34 ++++++++++++++++++ .../selftests/bpf/progs/test_kfunc_in_tp.c | 35 +++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/kfunc_in_tp.c create mode 100644 tools/testing/selftests/bpf/progs/test_kfunc_in_tp.c
Signature · 1 line
2.46.0
PATCH 1/2 bpf/selftests: coverage for calling kfuncs within tracepoint
This test exposes the issue of being unable to call kfuncs within a normal tracepoint program. The program will be rejected by the verifier as not allowed.
--- .../selftests/bpf/prog_tests/kfunc_in_tp.c | 34 ++++++++++++++++++ .../selftests/bpf/progs/test_kfunc_in_tp.c | 35 +++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/kfunc_in_tp.c create mode 100644 tools/testing/selftests/bpf/progs/test_kfunc_in_tp.c
tools/testing/selftests/bpf/prog_tests/kfunc_in_tp.c new file +34 -0
@@ -0,0 +1,34 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */ + +#include <errno.h> +#include <sys/syscall.h> +#include <unistd.h> + +#include "test_kfunc_in_tp.skel.h" +#include "test_progs.h" + +static void run_tp (void ) +{ + (void )syscall (__NR_getpid); +} + +void test_kfunc_in_tp (void ) +{ +struct test_kfunc_in_tp *skel; +int err; + + skel =test_kfunc_in_tp__open (); +ASSERT_OK_PTR (skel,"test_kfunc_in_tp__open" ); + + err =test_kfunc_in_tp__load (skel); +ASSERT_OK (err,"test_kfunc_in_tp__load" ); + + err =test_kfunc_in_tp__attach (skel); +ASSERT_OK (err,"test_kfunc_in_tp__attach" ); + +run_tp (); +ASSERT_OK (skel->data->result,"complete" ); + +test_kfunc_in_tp__destroy (skel); +}
tools/testing/selftests/bpf/progs/test_kfunc_in_tp.c new file +35 -0
@@ -0,0 +1,35 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */ + +#include "vmlinux.h" +#include <bpf/bpf_helpers.h> + +char _license[] SEC("license") = "GPL"; + +extern struct bpf_cpumask *bpf_cpumask_create (void ) __ksym; +extern void bpf_cpumask_set_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym; +extern bool bpf_cpumask_test_cpu(u32 cpu, const struct cpumask *cpumask) __ksym; +extern void bpf_cpumask_release(struct bpf_cpumask *cpumask) __ksym; + +int result =-1 ; + +/* call arbitrary kfuncs within a tracepoint program */ +SEC("tp/syscalls/sys_enter_getpid") +int handle_tp (struct trace_event_raw_ipi_send_cpumask *ctx) +{ +struct bpf_cpumask *cpumask; +const u32 cpu =bpf_get_smp_processor_id (); + + cpumask =bpf_cpumask_create (); +if (!cpumask) +return 0 ; + +bpf_cpumask_set_cpu (cpu, cpumask); +if (bpf_cpumask_test_cpu (cpu, (struct cpumask *)cpumask)) +bpf_printk ("match\n" ); + +bpf_cpumask_release (cpumask); + result =0 ; + +return 0 ; +}
Signature · 1 line
2.46.0
Eduard Zingerman
maintainerRe: [PATCH bpf-next 1/2] bpf/selftests: coverage for calling kfuncs within tracepoint
Quoted 6 lines · JP Kobryn
This test exposes the issue of being unable to call kfuncs within a normal tracepoint program. The program will be rejected by the verifier as not allowed.
Signed-off-by JP Kobryn <[email protected]>---
I have no opinion regarding the gist of this patch-set, just a few technical nits.
Please swap places for patches 1 and 2, such that at any point in commit history selftests are passing. This should help with any potential bisect.
Quoted 18 lines
.../selftests/bpf/prog_tests/kfunc_in_tp.c | 34 ++++++++++++++++++ .../selftests/bpf/progs/test_kfunc_in_tp.c | 35 +++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/kfunc_in_tp.c create mode 100644 tools/testing/selftests/bpf/progs/test_kfunc_in_tp.ctools/testing/selftests/bpf/prog_tests/kfunc_in_tp.c new file +6
-0@@ -0,0 +1,34 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */ + +#include <errno.h> +#include <sys/syscall.h> +#include <unistd.h>
tools/testing/selftests/bpf/prog_tests/kfunc_in_tp.c line 16
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/kfunc_in_tp.c new file +10
-0+ +#include "test_kfunc_in_tp.skel.h" +#include "test_progs.h" + +static void run_tp (void ) +{ + (void )syscall (__NR_getpid); +} + +void test_kfunc_in_tp (void )
Could you please consider use of test_loader framework for these tests? This would make 'kfunc_in_tp.c' unnecessary.
Example of tests using this framework:
tools/testing/selftests/bpf/progs/cpumask_failure.c
(but please, integrate with tools/testing/selftests/bpf/prog_tests/verifier.c
to avoid creation of unnecessary files).
Quoted 18 lines
tools/testing/selftests/bpf/prog_tests/kfunc_in_tp.c new file +18
-0+{ +struct test_kfunc_in_tp *skel; +int err; + + skel =test_kfunc_in_tp__open (); +ASSERT_OK_PTR (skel,"test_kfunc_in_tp__open" ); + + err =test_kfunc_in_tp__load (skel); +ASSERT_OK (err,"test_kfunc_in_tp__load" ); + + err =test_kfunc_in_tp__attach (skel); +ASSERT_OK (err,"test_kfunc_in_tp__attach" ); + +run_tp (); +ASSERT_OK (skel->data->result,"complete" ); + +test_kfunc_in_tp__destroy (skel); +}
[...]
PATCH 2/2 bpf: allow kfuncs within normal tracepoint programs
In reply to JP KobrynAccount for normal tracepoint programs by associating them with the kfunc tracing hook. This allows kfuncs to be called within tracepoint programs.
--- kernel/bpf/btf.c | 1 + 1 file changed, 1 insertion(+)
kernel/bpf/btf.c +1 -0
@@ -8303,6 +8303,7 @@ static int bpf_prog_type_to_kfunc_hook(enum bpf_prog_type prog_type)return BTF_KFUNC_HOOK_TC ;case BPF_PROG_TYPE_STRUCT_OPS :return BTF_KFUNC_HOOK_STRUCT_OPS ; +case BPF_PROG_TYPE_TRACEPOINT :case BPF_PROG_TYPE_TRACING :case BPF_PROG_TYPE_LSM :return BTF_KFUNC_HOOK_TRACING ;
Signature · 1 line
2.46.0
Re: [PATCH bpf-next 2/2] bpf: allow kfuncs within normal tracepoint programs
Quoted 22 lines · JP Kobryn
Account for normal tracepoint programs by associating them with the kfunc tracing hook. This allows kfuncs to be called within tracepoint programs.
Signed-off-by JP Kobryn <[email protected]>--- kernel/bpf/btf.c | 1 + 1 file changed, 1 insertion(+)kernel/bpf/btf.c +1
-1@@ -8303,6 +8303,7 @@ static int bpf_prog_type_to_kfunc_hook(enum bpf_prog_type prog_type)return BTF_KFUNC_HOOK_TC ;case BPF_PROG_TYPE_STRUCT_OPS :return BTF_KFUNC_HOOK_STRUCT_OPS ; +case BPF_PROG_TYPE_TRACEPOINT :case BPF_PROG_TYPE_TRACING :case BPF_PROG_TYPE_LSM :return BTF_KFUNC_HOOK_TRACING ; --
Quoted 2 lines
2.46.0
I'm not 100% sure it's ok to map TRACEPOINT prog type to HOOK_TRACING. But assuming it is, shouldn't we then also do the same for KPROBE and PERF_EVENT programs?
Either way, please consider Eduard's suggestion about changing patch order and switching to TEST_LOADER approach.
pw-bot: cr