diff mbox series

[bpf-next] libbpf: allow disabling auto attach

Message ID 20220816214945.742924-1-haoluo@google.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series [bpf-next] libbpf: allow disabling auto attach | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers success CCed 12 of 12 maintainers
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch warning WARNING: line length of 87 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 76 this patch: 76
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-4 success Logs for llvm-toolchain
bpf/vmtest-bpf-next-VM_Test-5 success Logs for set-matrix
bpf/vmtest-bpf-next-PR fail PR summary
bpf/vmtest-bpf-next-VM_Test-1 fail Logs for Kernel LATEST on ubuntu-latest with gcc
bpf/vmtest-bpf-next-VM_Test-2 fail Logs for Kernel LATEST on ubuntu-latest with llvm-16
bpf/vmtest-bpf-next-VM_Test-3 fail Logs for Kernel LATEST on z15 with gcc

Commit Message

Hao Luo Aug. 16, 2022, 9:49 p.m. UTC
Add 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 for them 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>
---
 tools/lib/bpf/libbpf.c | 16 ++++++++++++++++
 tools/lib/bpf/libbpf.h |  2 ++
 2 files changed, 18 insertions(+)

Comments

Andrii Nakryiko Aug. 16, 2022, 10 p.m. UTC | #1
On Tue, Aug 16, 2022 at 2:49 PM Hao Luo <haoluo@google.com> wrote:
>
> Add 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 for them 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>
> ---
>  tools/lib/bpf/libbpf.c | 16 ++++++++++++++++
>  tools/lib/bpf/libbpf.h |  2 ++
>  2 files changed, 18 insertions(+)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index aa05a99b913d..25f654d25b46 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -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;
> @@ -12349,6 +12362,9 @@ int bpf_object__attach_skeleton(struct bpf_object_skeleton *s)
>                 if (!prog->autoload)
>                         continue;
>
> +               if (!prog->autoattach)
> +                       continue;
> +

nit: I'd combine as if (!prog->autoload || !prog->autoattach), they
are very coupled in this sense

>                 /* auto-attaching not supported for this program */
>                 if (!prog->sec_def || !prog->sec_def->prog_attach_fn)
>                         continue;
> diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
> index 61493c4cddac..88a1ac34b12a 100644
> --- a/tools/lib/bpf/libbpf.h
> +++ b/tools/lib/bpf/libbpf.h
> @@ -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);

please add these APIs to libbpf.map as well

it would be also nice to have a simple test validating that skeleton's
auto-attach doesn't attach program (no link will be created) if
bpf_program__set_autoattach(false) is called before. Can you please
add that as well?

>
>  struct bpf_insn;
>
> --
> 2.37.1.595.g718a3a8f04-goog
>
Hao Luo Aug. 16, 2022, 10:16 p.m. UTC | #2
On Tue, Aug 16, 2022 at 3:01 PM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Tue, Aug 16, 2022 at 2:49 PM Hao Luo <haoluo@google.com> wrote:
> >
> > Add 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 for them 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>
> > ---
> >  tools/lib/bpf/libbpf.c | 16 ++++++++++++++++
> >  tools/lib/bpf/libbpf.h |  2 ++
> >  2 files changed, 18 insertions(+)
> >
> > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> > index aa05a99b913d..25f654d25b46 100644
> > --- a/tools/lib/bpf/libbpf.c
> > +++ b/tools/lib/bpf/libbpf.c
[...]
> >  const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog)
> >  {
> >         return prog->insns;
> > @@ -12349,6 +12362,9 @@ int bpf_object__attach_skeleton(struct bpf_object_skeleton *s)
> >                 if (!prog->autoload)
> >                         continue;
> >
> > +               if (!prog->autoattach)
> > +                       continue;
> > +
>
> nit: I'd combine as if (!prog->autoload || !prog->autoattach), they
> are very coupled in this sense
>

Sure.

> >                 /* auto-attaching not supported for this program */
> >                 if (!prog->sec_def || !prog->sec_def->prog_attach_fn)
> >                         continue;
> > diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
> > index 61493c4cddac..88a1ac34b12a 100644
> > --- a/tools/lib/bpf/libbpf.h
> > +++ b/tools/lib/bpf/libbpf.h
> > @@ -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);
>
> please add these APIs to libbpf.map as well
>

Ok. Which section? LIBBPF_1.0.0? Do the items in each section have a
particular order?

> it would be also nice to have a simple test validating that skeleton's
> auto-attach doesn't attach program (no link will be created) if
> bpf_program__set_autoattach(false) is called before. Can you please
> add that as well?
>

Ok. Will add a test and send v2.

> >
> >  struct bpf_insn;
> >
> > --
> > 2.37.1.595.g718a3a8f04-goog
> >
Andrii Nakryiko Aug. 16, 2022, 10:55 p.m. UTC | #3
On Tue, Aug 16, 2022 at 3:16 PM Hao Luo <haoluo@google.com> wrote:
>
> On Tue, Aug 16, 2022 at 3:01 PM Andrii Nakryiko
> <andrii.nakryiko@gmail.com> wrote:
> >
> > On Tue, Aug 16, 2022 at 2:49 PM Hao Luo <haoluo@google.com> wrote:
> > >
> > > Add 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 for them 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>
> > > ---
> > >  tools/lib/bpf/libbpf.c | 16 ++++++++++++++++
> > >  tools/lib/bpf/libbpf.h |  2 ++
> > >  2 files changed, 18 insertions(+)
> > >
> > > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> > > index aa05a99b913d..25f654d25b46 100644
> > > --- a/tools/lib/bpf/libbpf.c
> > > +++ b/tools/lib/bpf/libbpf.c
> [...]
> > >  const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog)
> > >  {
> > >         return prog->insns;
> > > @@ -12349,6 +12362,9 @@ int bpf_object__attach_skeleton(struct bpf_object_skeleton *s)
> > >                 if (!prog->autoload)
> > >                         continue;
> > >
> > > +               if (!prog->autoattach)
> > > +                       continue;
> > > +
> >
> > nit: I'd combine as if (!prog->autoload || !prog->autoattach), they
> > are very coupled in this sense
> >
>
> Sure.
>
> > >                 /* auto-attaching not supported for this program */
> > >                 if (!prog->sec_def || !prog->sec_def->prog_attach_fn)
> > >                         continue;
> > > diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
> > > index 61493c4cddac..88a1ac34b12a 100644
> > > --- a/tools/lib/bpf/libbpf.h
> > > +++ b/tools/lib/bpf/libbpf.h
> > > @@ -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);
> >
> > please add these APIs to libbpf.map as well
> >
>
> Ok. Which section? LIBBPF_1.0.0? Do the items in each section have a
> particular order?

Yes, 1.0.0 section. All the functions are sorted alphabetically.

> > it would be also nice to have a simple test validating that skeleton's
> > auto-attach doesn't attach program (no link will be created) if
> > bpf_program__set_autoattach(false) is called before. Can you please
> > add that as well?
> >
>
> Ok. Will add a test and send v2.
>
> > >
> > >  struct bpf_insn;
> > >
> > > --
> > > 2.37.1.595.g718a3a8f04-goog
> > >
Hao Luo Aug. 16, 2022, 11 p.m. UTC | #4
On Tue, Aug 16, 2022 at 3:55 PM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Tue, Aug 16, 2022 at 3:16 PM Hao Luo <haoluo@google.com> wrote:
> >
> > On Tue, Aug 16, 2022 at 3:01 PM Andrii Nakryiko
> > <andrii.nakryiko@gmail.com> wrote:
> > >
> > > On Tue, Aug 16, 2022 at 2:49 PM Hao Luo <haoluo@google.com> wrote:
> > > >
> > > > Add 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 for them 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>
> > > > ---
[...]
> > > > diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
> > > > index 61493c4cddac..88a1ac34b12a 100644
> > > > --- a/tools/lib/bpf/libbpf.h
> > > > +++ b/tools/lib/bpf/libbpf.h
> > > > @@ -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);
> > >
> > > please add these APIs to libbpf.map as well
> > >
> >
> > Ok. Which section? LIBBPF_1.0.0? Do the items in each section have a
> > particular order?
>
> Yes, 1.0.0 section. All the functions are sorted alphabetically.
>

Thanks for confirming. :)
diff mbox series

Patch

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index aa05a99b913d..25f654d25b46 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -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;
@@ -12349,6 +12362,9 @@  int bpf_object__attach_skeleton(struct bpf_object_skeleton *s)
 		if (!prog->autoload)
 			continue;
 
+		if (!prog->autoattach)
+			continue;
+
 		/* auto-attaching not supported for this program */
 		if (!prog->sec_def || !prog->sec_def->prog_attach_fn)
 			continue;
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index 61493c4cddac..88a1ac34b12a 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -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;