libbpf: Allows disabling auto attach
Subsystems (4)
PATCH v2 1/2 libbpf: Allows disabling auto attach
Adds libbpf APIs for disabling auto-attach for individual functions. This is motivated by the use case of cgroup iter [1]. Some iter types require their parameters to be non-zero, therefore applying auto-attach on them will fail. With these two new APIs, users who want to use auto-attach and these types of iters can disable auto-attach on the program and perform manual attach.
[1] https://lore.kernel.org/bpf/CAEf4BzZ+a2uDo_t6kGBziqdz--m2gh2_EUwkGLDtMd65uwxUjA@mail.gmail.com/
--- tools/lib/bpf/libbpf.c | 15 ++++++++++++++- tools/lib/bpf/libbpf.h | 2 ++ tools/lib/bpf/libbpf.map | 2 ++ 3 files changed, 18 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +14 -1
@@ -417,6 +417,7 @@ struct bpf_program {int fd;bool autoload; +bool autoattach;bool mark_btf_static;enum bpf_prog_type type;enum bpf_attach_type expected_attach_type;
@@ -755,6 +756,8 @@ bpf_object__init_prog(struct bpf_object *obj, struct bpf_program *prog, prog->autoload = true; } + prog->autoattach = true; +/* inherit object's log_level */ prog->log_level = obj->log_level;
@@ -8314,6 +8317,16 @@ int bpf_program__set_autoload(struct bpf_program *prog, bool autoload)return 0 ; } +bool bpf_program__autoattach (const struct bpf_program *prog) +{ +return prog->autoattach; +} + +void bpf_program__set_autoattach (struct bpf_program *prog,bool autoattach) +{ + prog->autoattach = autoattach; +} +const struct bpf_insn *bpf_program__insns (const struct bpf_program *prog) {return prog->insns;
@@ -12346,7 +12359,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->autoload) +if (!prog->autoload || !prog->autoattach)continue ;/* auto-attaching not supported for this program */
tools/lib/bpf/libbpf.h +2 -0
@@ -260,6 +260,8 @@ LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog); LIBBPF_API const char *bpf_program__section_name(const struct bpf_program *prog); LIBBPF_API bool bpf_program__autoload(const struct bpf_program *prog); LIBBPF_API int bpf_program__set_autoload(struct bpf_program *prog, bool autoload); +LIBBPF_API bool bpf_program__autoattach(const struct bpf_program *prog); +LIBBPF_API void bpf_program__set_autoattach(struct bpf_program *prog, bool autoattach);struct bpf_insn ;
tools/lib/bpf/libbpf.map +2 -0
@@ -358,6 +358,8 @@ LIBBPF_1.0.0 { bpf_obj_get_opts; bpf_prog_query_opts; bpf_program__attach_ksyscall; + bpf_program__autoattach; + bpf_program__set_autoattach; btf__add_enum64; btf__add_enum64_value; libbpf_bpf_attach_type_str;
Signature · 1 line
2.37.1.595.g718a3a8f04-goog
PATCH v2 2/2 selftests/bpf: Tests libbpf autoattach APIs
Adds test for libbpf APIs that toggle bpf program auto-attaching.
--- .../selftests/bpf/prog_tests/autoattach.c | 30 +++++++++++++++++++ .../selftests/bpf/progs/test_autoattach.c | 23 ++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/autoattach.c create mode 100644 tools/testing/selftests/bpf/progs/test_autoattach.c
tools/testing/selftests/bpf/prog_tests/autoattach.c new file +30 -0
@@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2022 Google */ + +#include <test_progs.h> +#include "test_autoattach.skel.h" + +void test_autoattach (void ) +{ +struct test_autoattach * skel; + + skel =test_autoattach__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel_open_and_load" )) + goto cleanup; + +/* disable auto-attach for prog2 */ +bpf_program__set_autoattach (skel->progs.prog2, false); +ASSERT_TRUE (bpf_program__autoattach (skel->progs.prog1),"autoattach_prog1" ); +ASSERT_FALSE (bpf_program__autoattach (skel->progs.prog2),"autoattach_prog2" ); +if (!ASSERT_OK (test_autoattach__attach (skel),"skel_attach" )) + goto cleanup; + +usleep (1 ); + +ASSERT_TRUE (skel->bss->prog1_called,"attached_prog1" ); +ASSERT_FALSE (skel->bss->prog2_called,"attached_prog2" ); + +cleanup: +test_autoattach__destroy (skel); +} +
tools/testing/selftests/bpf/progs/test_autoattach.c new file +23 -0
@@ -0,0 +1,23 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2022 Google */ + +#include "vmlinux.h" +#include <bpf/bpf_tracing.h> + +bool prog1_called = false; +bool prog2_called = false; + +SEC("raw_tp/sys_enter") +int prog1 (const void *ctx) +{ + prog1_called = true; +return 0 ; +} + +SEC("raw_tp/sys_exit") +int prog2 (const void *ctx) +{ + prog2_called = true; +return 0 ; +} +
Signature · 1 line
2.37.1.595.g718a3a8f04-goog
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Quoted 8 lines · On Tue, 16 Aug 2022 16:40:11 -0700 you wrote:
Adds libbpf APIs for disabling auto-attach for individual functions. This is motivated by the use case of cgroup iter [1]. Some iter types require their parameters to be non-zero, therefore applying auto-attach on them will fail. With these two new APIs, users who want to use auto-attach and these types of iters can disable auto-attach on the program and perform manual attach.
[...]
Here is the summary with links:
- [bpf-next,v2,1/2] libbpf: Allows disabling auto attach
https://git.kernel.org/bpf/bpf-next/c/43cb8cbadffa - [bpf-next,v2,2/2] selftests/bpf: Tests libbpf autoattach APIs https://git.kernel.org/bpf/bpf-next/c/738a2f2f9130
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html