libbpf: support opting out from autoloading BPF programs declaratively
Subsystems (4)
Andrii Nakryiko
maintainerPATCH 1/2 libbpf: support opting out from autoloading BPF programs declaratively
Establish SEC("?abc") naming convention (i.e., adding question mark in front of otherwise normal section name) that allows to set corresponding program's autoload property to false. This is effectively just a declarative way to do bpf_program__set_autoload(prog, false).
Having a way to do this declaratively in BPF code itself is useful and convenient for various scenarios. E.g., for testing, when BPF object consists of multiple independent BPF programs that each needs to be tested separately. Opting out all of them by default and then setting autoload to true for just one of them at a time simplifies testing code (see next patch for few conversions in BPF selftests taking advantage of this new feature).
Another real-world use case is in libbpf-tools for cases when different BPF programs have to be picked depending on particulars of the host kernel due to various incompatible changes (like kernel function renames or signature change, or to pick kprobe vs fentry depending on corresponding kernel support for the latter). Marking all the different BPF program candidates as non-autoloaded declaratively makes this more obvious in BPF source code and allows simpler code in user-space code.
When BPF program marked as SEC("?abc") it is otherwise treated just like SEC("abc") and bpf_program__section_name() reported will be "abc".
--- tools/lib/bpf/libbpf.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-)
tools/lib/bpf/libbpf.c +21 -10
@@ -302,7 +302,7 @@ struct bpf_program {void *priv;bpf_program_clear_priv_t clear_priv; -bool load; +bool autoload;bool mark_btf_static;enum bpf_prog_type type;enum bpf_attach_type expected_attach_type;
@@ -672,7 +672,18 @@ bpf_object__init_prog(struct bpf_object *obj, struct bpf_program *prog, prog->insns_cnt = prog->sec_insn_cnt; prog->type =BPF_PROG_TYPE_UNSPEC ; - prog->load = true; + +/* libbpf's convention for SEC("?abc...") is that it's just like +* SEC("abc...") but the corresponding bpf_program starts out with +* autoload set to false. +*/ +if (sec_name[0 ] =='?' ) { + prog->autoload = false; +/* from now on forget there was ? in section name */ + sec_name++; + }else { + prog->autoload = true; + } prog->instances.fds =NULL ; prog->instances.nr =-1 ;
@@ -2927,7 +2938,7 @@ static bool obj_needs_vmlinux_btf(const struct bpf_object *obj) }bpf_object__for_each_program (prog, obj) { -if (!prog->load) +if (!prog->autoload)continue ;if (prog_needs_vmlinux_btf (prog))return true;
@@ -5702,7 +5713,7 @@ bpf_object__relocate_core(struct bpf_object *obj, const char *targ_btf_path)/* no need to apply CO-RE relocation if the program is * not going to be loaded */ -if (!prog->load) +if (!prog->autoload)continue ;/* adjust insn_idx from section frame of reference to the local
@@ -6363,7 +6374,7 @@ bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_path)*/ if (prog_is_subprog (obj, prog))continue ; -if (!prog->load) +if (!prog->autoload)continue ; err =bpf_object__relocate_calls (obj, prog);
@@ -6378,7 +6389,7 @@ bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_path) prog = &obj->programs[i];if (prog_is_subprog (obj, prog))continue ; -if (!prog->load) +if (!prog->autoload)continue ; err =bpf_object__relocate_data (obj, prog);if (err) {
@@ -6975,7 +6986,7 @@ bpf_object__load_progs(struct bpf_object *obj, int log_level) prog = &obj->programs[i];if (prog_is_subprog (obj, prog))continue ; -if (!prog->load) { +if (!prog->autoload) {pr_debug ("prog '%s': skipped loading\n" , prog->name);continue ; }
@@ -8455,7 +8466,7 @@ const char *bpf_program__title(const struct bpf_program *prog, bool needs_copy)bool bpf_program__autoload (const struct bpf_program *prog) { -return prog->load; +return prog->autoload; }int bpf_program__set_autoload (struct bpf_program *prog,bool autoload)
@@ -8463,7 +8474,7 @@ int bpf_program__set_autoload(struct bpf_program *prog, bool autoload)if (prog->obj->loaded)return libbpf_err (-EINVAL ); - prog->load = autoload; + prog->autoload = autoload;return 0 ; }
@@ -12665,7 +12676,7 @@ int bpf_object__attach_skeleton(struct bpf_object_skeleton *s)struct bpf_program *prog = *s->progs[i].prog;struct bpf_link **link = s->progs[i].link; -if (!prog->load) +if (!prog->autoload)continue ;/* auto-attaching not supported for this program */
Signature · 1 line
2.30.2
Andrii Nakryiko
maintainerPATCH 2/2 selftests/bpf: use non-autoloaded programs in few tests
Take advantage of new libbpf feature for declarative non-autoloaded BPF program SEC() definitions in few test that test single program at a time out of many available programs within the single BPF object.
--- .../bpf/prog_tests/helper_restricted.c | 10 +++----- .../bpf/prog_tests/reference_tracking.c | 23 ++++++----------- .../selftests/bpf/prog_tests/test_strncmp.c | 25 +++---------------- .../selftests/bpf/progs/strncmp_test.c | 8 +++--- .../bpf/progs/test_helper_restricted.c | 16 ++++++------ .../selftests/bpf/progs/test_sk_lookup_kern.c | 18 ++++++------- 6 files changed, 35 insertions(+), 65 deletions(-)
tools/testing/selftests/bpf/prog_tests/helper_restricted.c +4 -6
@@ -6,11 +6,10 @@void test_helper_restricted (void ) {int prog_i =0 , prog_cnt; -int duration =0 ; do { struct test_helper_restricted *test; - int maybeOK; + int err; test = test_helper_restricted__open(); if (!ASSERT_OK_PTR(test, "open"))
@@ -21,12 +20,11 @@ void test_helper_restricted(void)for (int j =0 ; j < prog_cnt; ++j) {struct bpf_program *prog = *test->skeleton->progs[j].prog; - maybeOK =bpf_program__set_autoload (prog, prog_i == j); -ASSERT_OK (maybeOK,"set autoload" ); +bpf_program__set_autoload (prog, true); } - maybeOK =test_helper_restricted__load (test); -CHECK (!maybeOK, test->skeleton->progs[prog_i].name,"helper isn't restricted" ); + err =test_helper_restricted__load (test); +ASSERT_ERR (err,"load_should_fail" );test_helper_restricted__destroy (test); }while (++prog_i < prog_cnt);
tools/testing/selftests/bpf/prog_tests/reference_tracking.c +7 -16
@@ -1,21 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #include <test_progs.h> -static void toggle_object_autoload_progs (const struct bpf_object *obj, -const char *name_load) -{ -struct bpf_program *prog; - -bpf_object__for_each_program (prog, obj) { -const char *name =bpf_program__name (prog); - -if (!strcmp (name_load, name)) -bpf_program__set_autoload (prog, true); -else -bpf_program__set_autoload (prog, false); - } -} -void test_reference_tracking (void ) {const char *file ="test_sk_lookup_kern.o" ;
@@ -39,6 +24,7 @@ void test_reference_tracking(void) goto cleanup;bpf_object__for_each_program (prog, obj_iter) { +struct bpf_program *p;const char *name; name =bpf_program__name (prog);
@@ -49,7 +35,12 @@ void test_reference_tracking(void)if (!ASSERT_OK_PTR (obj,"obj_open_file" )) goto cleanup; -toggle_object_autoload_progs (obj, name); +/* all programs are not loaded by default, so just set +* autoload to true for the single prog under test +*/ + p =bpf_object__find_program_by_name (obj, name); +bpf_program__set_autoload (p, true); +/* Expect verifier failure if test name has 'err' */ if (strncmp (name,"err_" ,sizeof ("err_" ) -1 ) ==0 ) {libbpf_print_fn_t old_print_fn;
tools/testing/selftests/bpf/prog_tests/test_strncmp.c +3 -22
@@ -44,16 +44,12 @@ static void strncmp_full_str_cmp(struct strncmp_test *skel, const char *name,static void test_strncmp_ret (void ) {struct strncmp_test *skel; -struct bpf_program *prog;int err, got; skel =strncmp_test__open ();if (!ASSERT_OK_PTR (skel,"strncmp_test open" ))return ; -bpf_object__for_each_program (prog, skel->obj) -bpf_program__set_autoload (prog, false); -bpf_program__set_autoload (skel->progs.do_strncmp, true); err =strncmp_test__load (skel);
@@ -91,18 +87,13 @@ static void test_strncmp_ret(void)static void test_strncmp_bad_not_const_str_size (void ) {struct strncmp_test *skel; -struct bpf_program *prog;int err; skel =strncmp_test__open ();if (!ASSERT_OK_PTR (skel,"strncmp_test open" ))return ; -bpf_object__for_each_program (prog, skel->obj) -bpf_program__set_autoload (prog, false); - -bpf_program__set_autoload (skel->progs.strncmp_bad_not_const_str_size, - true); +bpf_program__set_autoload (skel->progs.strncmp_bad_not_const_str_size, true); err =strncmp_test__load (skel);ASSERT_ERR (err,"strncmp_test load bad_not_const_str_size" );
@@ -113,18 +104,13 @@ static void test_strncmp_bad_not_const_str_size(void)static void test_strncmp_bad_writable_target (void ) {struct strncmp_test *skel; -struct bpf_program *prog;int err; skel =strncmp_test__open ();if (!ASSERT_OK_PTR (skel,"strncmp_test open" ))return ; -bpf_object__for_each_program (prog, skel->obj) -bpf_program__set_autoload (prog, false); - -bpf_program__set_autoload (skel->progs.strncmp_bad_writable_target, - true); +bpf_program__set_autoload (skel->progs.strncmp_bad_writable_target, true); err =strncmp_test__load (skel);ASSERT_ERR (err,"strncmp_test load bad_writable_target" );
@@ -135,18 +121,13 @@ static void test_strncmp_bad_writable_target(void)static void test_strncmp_bad_not_null_term_target (void ) {struct strncmp_test *skel; -struct bpf_program *prog;int err; skel =strncmp_test__open ();if (!ASSERT_OK_PTR (skel,"strncmp_test open" ))return ; -bpf_object__for_each_program (prog, skel->obj) -bpf_program__set_autoload (prog, false); - -bpf_program__set_autoload (skel->progs.strncmp_bad_not_null_term_target, - true); +bpf_program__set_autoload (skel->progs.strncmp_bad_not_null_term_target, true); err =strncmp_test__load (skel);ASSERT_ERR (err,"strncmp_test load bad_not_null_term_target" );
tools/testing/selftests/bpf/progs/strncmp_test.c +4 -4
@@ -19,7 +19,7 @@ unsigned int no_const_str_size = STRNCMP_STR_SZ; char _license[] SEC("license") = "GPL"; -SEC("tp/syscalls/sys_enter_nanosleep") +SEC("?tp/syscalls/sys_enter_nanosleep")int do_strncmp (void *ctx) {if ((bpf_get_current_pid_tgid () >>32 ) != target_pid)
@@ -29,7 +29,7 @@ int do_strncmp(void *ctx)return 0 ; } -SEC("tp/syscalls/sys_enter_nanosleep") +SEC("?tp/syscalls/sys_enter_nanosleep")int strncmp_bad_not_const_str_size (void *ctx) {/* The value of string size is not const, so will fail */
@@ -37,7 +37,7 @@ int strncmp_bad_not_const_str_size(void *ctx)return 0 ; } -SEC("tp/syscalls/sys_enter_nanosleep") +SEC("?tp/syscalls/sys_enter_nanosleep")int strncmp_bad_writable_target (void *ctx) {/* Compared target is not read-only, so will fail */
@@ -45,7 +45,7 @@ int strncmp_bad_writable_target(void *ctx)return 0 ; } -SEC("tp/syscalls/sys_enter_nanosleep") +SEC("?tp/syscalls/sys_enter_nanosleep")int strncmp_bad_not_null_term_target (void *ctx) {/* Compared target is not null-terminated, so will fail */
tools/testing/selftests/bpf/progs/test_helper_restricted.c +8 -8
@@ -56,7 +56,7 @@ static void spin_lock_work(void) } } -SEC("raw_tp/sys_enter") +SEC("?raw_tp/sys_enter")int raw_tp_timer (void *ctx) {timer_work ();
@@ -64,7 +64,7 @@ int raw_tp_timer(void *ctx)return 0 ; } -SEC("tp/syscalls/sys_enter_nanosleep") +SEC("?tp/syscalls/sys_enter_nanosleep")int tp_timer (void *ctx) {timer_work ();
@@ -72,7 +72,7 @@ int tp_timer(void *ctx)return 0 ; } -SEC("kprobe/sys_nanosleep") +SEC("?kprobe/sys_nanosleep")int kprobe_timer (void *ctx) {timer_work ();
@@ -80,7 +80,7 @@ int kprobe_timer(void *ctx)return 0 ; } -SEC("perf_event") +SEC("?perf_event")int perf_event_timer (void *ctx) {timer_work ();
@@ -88,7 +88,7 @@ int perf_event_timer(void *ctx)return 0 ; } -SEC("raw_tp/sys_enter") +SEC("?raw_tp/sys_enter")int raw_tp_spin_lock (void *ctx) {spin_lock_work ();
@@ -96,7 +96,7 @@ int raw_tp_spin_lock(void *ctx)return 0 ; } -SEC("tp/syscalls/sys_enter_nanosleep") +SEC("?tp/syscalls/sys_enter_nanosleep")int tp_spin_lock (void *ctx) {spin_lock_work ();
@@ -104,7 +104,7 @@ int tp_spin_lock(void *ctx)return 0 ; } -SEC("kprobe/sys_nanosleep") +SEC("?kprobe/sys_nanosleep")int kprobe_spin_lock (void *ctx) {spin_lock_work ();
@@ -112,7 +112,7 @@ int kprobe_spin_lock(void *ctx)return 0 ; } -SEC("perf_event") +SEC("?perf_event")int perf_event_spin_lock (void *ctx) {spin_lock_work ();
tools/testing/selftests/bpf/progs/test_sk_lookup_kern.c +9 -9
@@ -52,7 +52,7 @@ static struct bpf_sock_tuple *get_tuple(void *data, __u64 nh_off,return result; } -SEC("tc") +SEC("?tc")int sk_lookup_success (struct __sk_buff *skb) {void *data_end = (void *)(long )skb->data_end;
@@ -78,7 +78,7 @@ int sk_lookup_success(struct __sk_buff *skb)return sk ?TC_ACT_OK :TC_ACT_UNSPEC ; } -SEC("tc") +SEC("?tc")int sk_lookup_success_simple (struct __sk_buff *skb) {struct bpf_sock_tuple tuple = {};
@@ -90,7 +90,7 @@ int sk_lookup_success_simple(struct __sk_buff *skb)return 0 ; } -SEC("tc") +SEC("?tc")int err_use_after_free (struct __sk_buff *skb) {struct bpf_sock_tuple tuple = {};
@@ -105,7 +105,7 @@ int err_use_after_free(struct __sk_buff *skb)return family; } -SEC("tc") +SEC("?tc")int err_modify_sk_pointer (struct __sk_buff *skb) {struct bpf_sock_tuple tuple = {};
@@ -120,7 +120,7 @@ int err_modify_sk_pointer(struct __sk_buff *skb)return 0 ; } -SEC("tc") +SEC("?tc")int err_modify_sk_or_null_pointer (struct __sk_buff *skb) {struct bpf_sock_tuple tuple = {};
@@ -134,7 +134,7 @@ int err_modify_sk_or_null_pointer(struct __sk_buff *skb)return 0 ; } -SEC("tc") +SEC("?tc")int err_no_release (struct __sk_buff *skb) {struct bpf_sock_tuple tuple = {};
@@ -143,7 +143,7 @@ int err_no_release(struct __sk_buff *skb)return 0 ; } -SEC("tc") +SEC("?tc")int err_release_twice (struct __sk_buff *skb) {struct bpf_sock_tuple tuple = {};
@@ -155,7 +155,7 @@ int err_release_twice(struct __sk_buff *skb)return 0 ; } -SEC("tc") +SEC("?tc")int err_release_unchecked (struct __sk_buff *skb) {struct bpf_sock_tuple tuple = {};
@@ -172,7 +172,7 @@ void lookup_no_release(struct __sk_buff *skb)bpf_sk_lookup_tcp (skb, &tuple,sizeof (tuple),BPF_F_CURRENT_NETNS ,0 ); } -SEC("tc") +SEC("?tc")int err_no_release_subcall (struct __sk_buff *skb) {lookup_no_release (skb);
Signature · 1 line
2.30.2
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 13 lines · On Mon, 18 Apr 2022 17:24:50 -0700 you wrote:
Establish
SEC("?abc")naming convention (i.e., adding question mark in front of otherwise normal section name) that allows to set corresponding program's autoload property to false. This is effectively just a declarative way to dobpf_program__set_autoload(prog, false).Having a way to do this declaratively in BPF code itself is useful and convenient for various scenarios. E.g., for testing, when BPF object consists of multiple independent BPF programs that each needs to be tested separately. Opting out all of them by default and then setting autoload to true for just one of them at a time simplifies testing code (see next patch for few conversions in BPF selftests taking advantage of this new feature).
Quoted 1 line
[...]
Here is the summary with links:
- [bpf-next,1/2] libbpf: support opting out from autoloading BPF programs declaratively
https://git.kernel.org/bpf/bpf-next/c/a3820c481112 - [bpf-next,2/2] selftests/bpf: use non-autoloaded programs in few tests https://git.kernel.org/bpf/bpf-next/c/0d7fefebea55
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html