Skip to contentSkip to conversation

libbpf: allow disabling auto attach

PATCH Superseded Hao Luo 2 participants 5 messages Aug 16, 2022

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Hao Luo

author
#0 lore raw

PATCH 1/1 libbpf: allow disabling auto attach

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/

---
 tools/lib/bpf/libbpf.c | 16 ++++++++++++++++
 tools/lib/bpf/libbpf.h |  2 ++
 2 files changed, 18 insertions(+)
tools/lib/bpf/libbpf.c +16 -0
index aa05a99b913d..25f654d25b46 100644
@@ -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;
tools/lib/bpf/libbpf.h +2 -0
index 61493c4cddac..88a1ac34b12a 100644
@@ -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;
 
Signature · 1 line
2.37.1.595.g718a3a8f04-goog
#1 lore raw
Quoted 55 lines · Hao Luo

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/

---
 tools/lib/bpf/libbpf.c | 16 ++++++++++++++++
 tools/lib/bpf/libbpf.h |  2 ++
 2 files changed, 18 insertions(+)
tools/lib/bpf/libbpf.c +13 -0
index aa05a99b913d..25f654d25b46 100644
@@ -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;

tools/lib/bpf/libbpf.c int bpf_object__attach_skeleton(struct bpf_object_skeleton *s) · line 12367

Quoted 6 lines
tools/lib/bpf/libbpf.c +3 -0
@@ -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

tools/lib/bpf/libbpf.h LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog); · line 264

Quoted 13 lines
tools/lib/bpf/libbpf.c +0 -0
                /* auto-attaching not supported for this program */
                if (!prog->sec_def || !prog->sec_def->prog_attach_fn)
                        continue;
tools/lib/bpf/libbpf.h +2 -0
index 61493c4cddac..88a1ac34b12a 100644
@@ -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?

Quoted 6 lines
tools/lib/bpf/libbpf.h +0 -1
 struct bpf_insn;
 
--

2.37.1.595.g718a3a8f04-goog

author
#2 lore raw
Quoted 22 lines · Andrii Nakryiko
Quoted 20 lines · Hao Luo

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/

---
 tools/lib/bpf/libbpf.c | 16 ++++++++++++++++
 tools/lib/bpf/libbpf.h |  2 ++
 2 files changed, 18 insertions(+)
tools/lib/bpf/libbpf.c +0 -0
index aa05a99b913d..25f654d25b46 100644

[...]

Quoted 7 lines
Quoted 7 lines

const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog)

 {
        return prog->insns;
tools/lib/bpf/libbpf.c +0 -0
@@ -12349,6 +12362,9 @@ int bpf_object__attach_skeleton(struct bpf_object_skeleton *s)
                if (!prog->autoload)
                        continue;
 
Quoted 7 lines

tools/lib/bpf/libbpf.c int bpf_object__attach_skeleton(struct bpf_object_skeleton *s) · line 12367

Quoted 3 lines
tools/lib/bpf/libbpf.c +3 -0
+               if (!prog->autoattach)
+                       continue;
+

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

Sure.

Quoted 7 lines
Quoted 7 lines
tools/lib/bpf/libbpf.c +0 -0
                /* auto-attaching not supported for this program */
                if (!prog->sec_def || !prog->sec_def->prog_attach_fn)
                        continue;
tools/lib/bpf/libbpf.h +0 -0
index 61493c4cddac..88a1ac34b12a 100644
Quoted 9 lines

tools/lib/bpf/libbpf.h LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog); · line 264

Quoted 6 lines
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);

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?

Quoted 5 lines

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.

Quoted 6 lines
Quoted 6 lines
tools/lib/bpf/libbpf.h +0 -1
 struct bpf_insn;
 
--

2.37.1.595.g718a3a8f04-goog

#3 lore raw
Quoted 60 lines · Hao Luo
Quoted 22 lines · Andrii Nakryiko
Quoted 20 lines · Hao Luo

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/

---
 tools/lib/bpf/libbpf.c | 16 ++++++++++++++++
 tools/lib/bpf/libbpf.h |  2 ++
 2 files changed, 18 insertions(+)
tools/lib/bpf/libbpf.c +0 -0
index aa05a99b913d..25f654d25b46 100644

[...]

Quoted 7 lines
Quoted 7 lines

const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog)

 {
        return prog->insns;
tools/lib/bpf/libbpf.c +0 -0
@@ -12349,6 +12362,9 @@ int bpf_object__attach_skeleton(struct bpf_object_skeleton *s)
                if (!prog->autoload)
                        continue;
 
Quoted 7 lines

tools/lib/bpf/libbpf.c int bpf_object__attach_skeleton(struct bpf_object_skeleton *s) · line 12367

Quoted 3 lines
tools/lib/bpf/libbpf.c +3 -0
+               if (!prog->autoattach)
+                       continue;
+

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

Sure.

Quoted 7 lines
Quoted 7 lines
tools/lib/bpf/libbpf.c +0 -0
                /* auto-attaching not supported for this program */
                if (!prog->sec_def || !prog->sec_def->prog_attach_fn)
                        continue;
tools/lib/bpf/libbpf.h +0 -0
index 61493c4cddac..88a1ac34b12a 100644
Quoted 9 lines

tools/lib/bpf/libbpf.h LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog); · line 264

Quoted 6 lines
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);

please add these APIs to libbpf.map as well

Quoted 2 lines

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.

Quoted 14 lines
Quoted 5 lines

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.

Quoted 6 lines
Quoted 6 lines
tools/lib/bpf/libbpf.h +0 -1
 struct bpf_insn;
 
--

2.37.1.595.g718a3a8f04-goog

author
#4 lore raw
Quoted 19 lines · Andrii Nakryiko
Quoted 17 lines · Hao Luo
Quoted 14 lines · Andrii Nakryiko
Quoted 12 lines · Hao Luo

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/

---

[...]

Quoted 14 lines
Quoted 14 lines
Quoted 13 lines

tools/lib/bpf/libbpf.h LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog); · line 264

Quoted 10 lines
tools/lib/bpf/libbpf.h +2 -0
index 61493c4cddac..88a1ac34b12a 100644
@@ -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

Quoted 5 lines
Quoted 2 lines

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. :)