Skip to content

Commit

Permalink
libbpf: Allows disabling auto attach
Browse files Browse the repository at this point in the history
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/

Signed-off-by: Hao Luo <haoluo@google.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20220816234012.910255-1-haoluo@google.com
  • Loading branch information
Hao Luo authored and Andrii Nakryiko committed Aug 17, 2022
1 parent 807662c commit 43cb8cb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
15 changes: 14 additions & 1 deletion tools/lib/bpf/libbpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down
2 changes: 2 additions & 0 deletions tools/lib/bpf/libbpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 2 additions & 0 deletions tools/lib/bpf/libbpf.map
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 43cb8cb

Please sign in to comment.