Skip to contentSkip to conversation

libbpf: Add bpf_program__add_flags() and bpf_program__clear_flags()

PATCH Superseded Toke Høiland-Jørgensen 5 participants 14 messages Sep 10 – Sep 12

Subsystems (8)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/4 libbpf: Add bpf_program__add_flags() and bpf_program__clear_flags()

When changing BPF program flags, applications often need to just add or remove a single flag, without touching the existing ones. When using the bpf_program__set_flags() function, this requires reading the existing flags and doing bitwise manipulations before resetting the result, which is slightly awkward to do, and easy to forget.

Add two new convenience helpers, bpf_program__add_flags() and bpf_program__clear_flags(), which wraps bpf_program__set_flags() in the bitwise operations required to modify flags without clobbering the existing ones.

---
 tools/lib/bpf/libbpf.c   | 10 ++++++++++
 tools/lib/bpf/libbpf.h   |  6 ++++++
 tools/lib/bpf/libbpf.map |  2 ++
 3 files changed, 18 insertions(+)
tools/lib/bpf/libbpf.c +10 -0
index c036e8a91ed8..221c4fe1fb31 100644
@@ -9938,6 +9938,16 @@ int bpf_program__set_flags(struct bpf_program *prog, __u32 flags)
 	return 0;
 }
 
+int bpf_program__add_flags(struct bpf_program *prog, __u32 flags)
+{
+	return bpf_program__set_flags(prog, prog->prog_flags | flags);
+}
+
+int bpf_program__clear_flags(struct bpf_program *prog, __u32 flags)
+{
+	return bpf_program__set_flags(prog, prog->prog_flags & ~flags);
+}
+
 __u32 bpf_program__log_level(const struct bpf_program *prog)
 {
 	return prog->log_level;
tools/lib/bpf/libbpf.h +6 -0
index b965ad571540..3932bf9cb490 100644
@@ -1011,6 +1011,12 @@ bpf_program__set_expected_attach_type(struct bpf_program *prog,
 LIBBPF_API __u32 bpf_program__flags(const struct bpf_program *prog);
 LIBBPF_API int bpf_program__set_flags(struct bpf_program *prog, __u32 flags);
 
+/* Convenience helpers to non-destructively add or clear the specified flags
+ * instead of clobbering them as bpf_program__set_flags() does.
+ */
+LIBBPF_API int bpf_program__add_flags(struct bpf_program *prog, __u32 flags);
+LIBBPF_API int bpf_program__clear_flags(struct bpf_program *prog, __u32 flags);
+
 /* Per-program log level and log buffer getters/setters.
  * See bpf_object_open_opts comments regarding log_level and log_buf
  * interactions.
tools/lib/bpf/libbpf.map +2 -0
index 08ab2ea881fb..a811a1b3a085 100644
@@ -458,7 +458,9 @@ LIBBPF_1.7.0 {
 
 LIBBPF_1.8.0 {
 	global:
+		bpf_program__add_flags;
 		bpf_program__attach_tracing_multi;
+		bpf_program__clear_flags;
 		bpf_program__clone;
 		btf__find_by_name_kind_own;
 		btf__new_empty_opts;
Signature · 1 line
2.55.0
#1 lore raw

PATCH v2 2/4 selftests/bpf: Add assertions for bpf_program__{add,clear}_flags()

Add assertions that round-tripping through bpf_program__add_flags() and bpf_program__clear_flags() ends up with the original flags value. Arbitrarily add these to the kernel_flag test prog since that's where we're already checking for the bpf_program__flags() value (and the test name contains "flag").

---
 .../selftests/bpf/prog_tests/kernel_flag.c    | 20 +++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/kernel_flag.c +18 -2
index 25eb59f460ab..72c0ef8da409 100644
@@ -10,15 +10,31 @@ void test_kernel_flag(void)
 	struct test_kernel_flag *lsm_skel;
 	struct kfunc_call_test *skel = NULL;
 	struct kfunc_call_test_lskel *lskel = NULL;
+	struct bpf_program *prog;
+	__u32 flags;
 	int ret;
 
-	lsm_skel = test_kernel_flag__open_and_load();
+	lsm_skel = test_kernel_flag__open();
 	if (!ASSERT_OK_PTR(lsm_skel, "lsm_skel"))
 		return;
 
-	ASSERT_EQ(bpf_program__flags(lsm_skel->progs.bpf) & BPF_F_SLEEPABLE,
+	prog = lsm_skel->progs.bpf;
+	flags = bpf_program__flags(prog);
+	ASSERT_EQ(flags & BPF_F_SLEEPABLE,
 		  BPF_F_SLEEPABLE, "sleepable in program flags");
 
+	ret = bpf_program__add_flags(prog, BPF_F_ANY_ALIGNMENT);
+	ASSERT_OK(ret, "bpf_program__add_flags ret");
+	ASSERT_EQ(bpf_program__flags(prog), flags | BPF_F_ANY_ALIGNMENT,
+		  "bpf_program__add_flags value");
+
+	ret = bpf_program__clear_flags(prog, BPF_F_ANY_ALIGNMENT);
+	ASSERT_OK(ret, "bpf_program__clear_flags ret");
+	ASSERT_EQ(bpf_program__flags(prog), flags, "bpf_program__clear_flags value");
+
+	ret = test_kernel_flag__load(lsm_skel);
+	ASSERT_OK(ret, "test_kernel_flag__load");
+
 	lsm_skel->bss->monitored_tid = sys_gettid();
 
 	ret = test_kernel_flag__attach(lsm_skel);
Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH bpf-next v2 2/4] selftests/bpf: Add assertions for bpf_program__{add,clear}_flags()

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Missing early exit check on BPF skeleton load and flag manipulation operations allows execution to…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Missing early exit check on BPF skeleton load and flag manipulation operations allows execution to proceed in an invalid state. --

commit 1694db036efd5301fa9d3722ffe0c0854fa291b9
Author: Toke Høiland-Jørgensen <[email protected]>

selftests/bpf: Add assertions for bpf_program__{add,clear}_flags()

Add assertions that round-tripping through bpf_program__add_flags() and bpf_program__clear_flags() ends up with the original flags value. Arbitrarily add these to the kernel_flag test prog since that's where we're already checking for the bpf_program__flags() value (and the test name contains "flag").

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/kernel_flag.c +0 -0
index 25eb59f460abe..72c0ef8da409e 100644
@@ -10,15 +10,31 @@ void test_kernel_flag(void)

[ ... ]

tools/testing/selftests/bpf/prog_tests/kernel_flag.c void test_kernel_flag(void) · line 27

Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/kernel_flag.c +2 -0
+	ret = bpf_program__add_flags(prog, BPF_F_ANY_ALIGNMENT);
+	ASSERT_OK(ret, "bpf_program__add_flags ret");

[Severity: Low]
Should this ASSERT_OK() call be wrapped in an if (!...) goto close_prog; check? If it fails, the test will continue executing instead of cleanly aborting.

tools/testing/selftests/bpf/prog_tests/kernel_flag.c void test_kernel_flag(void) · line 36

Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/kernel_flag.c +9 -0
+	ASSERT_EQ(bpf_program__flags(prog), flags | BPF_F_ANY_ALIGNMENT,
+		  "bpf_program__add_flags value");
+
+	ret = bpf_program__clear_flags(prog, BPF_F_ANY_ALIGNMENT);
+	ASSERT_OK(ret, "bpf_program__clear_flags ret");
+	ASSERT_EQ(bpf_program__flags(prog), flags, "bpf_program__clear_flags value");
+
+	ret = test_kernel_flag__load(lsm_skel);
+	ASSERT_OK(ret, "test_kernel_flag__load");

[Severity: Low]
Is it intentional to allow execution to proceed if the skeleton fails to load? Wrapping this in if (!ASSERT_OK(ret, ...)) goto close_prog; would abort the test cleanly and avoid cascading test failures.

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/kernel_flag.c +1 -0
+
 	lsm_skel->bss->monitored_tid = sys_gettid();
 
 	ret = test_kernel_flag__attach(lsm_skel);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#3 lore raw

PATCH v2 3/4 selftests/bpf: Adopt bpf_program__add_flags() helper

In reply to Toke Høiland-Jørgensen

Adopt the newly added bpf_program__add_flags() helper everywhere the selftests non-destructively modifies program flags (which turns out to be every use of bpf_program__set_flags() in the tests).

---
 tools/testing/selftests/bpf/prog_tests/attach_probe.c      | 4 +---
 tools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c   | 2 +-
 tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c | 3 +--
 tools/testing/selftests/bpf/prog_tests/xdp_metadata.c      | 4 ++--
 tools/testing/selftests/bpf/test_loader.c                  | 5 ++---
 tools/testing/selftests/bpf/testing_helpers.c              | 4 +---
 tools/testing/selftests/bpf/veristat.c                     | 4 ++--
 tools/testing/selftests/bpf/xdp_hw_metadata.c              | 2 +-
 8 files changed, 11 insertions(+), 17 deletions(-)
tools/testing/selftests/bpf/prog_tests/attach_probe.c +1 -3
index 7dadb90e7b68..41136e1c7752 100644
@@ -543,9 +543,7 @@ static void test_kprobe_sleepable(void)
 		return;
 
 	/* sleepable kprobe test case needs flags set before loading */
-	if (!ASSERT_OK(bpf_program__set_flags(
-			       skel->progs.handle_kprobe_sleepable,
-			       bpf_program__flags(skel->progs.handle_kprobe_sleepable) | BPF_F_SLEEPABLE),
+	if (!ASSERT_OK(bpf_program__add_flags(skel->progs.handle_kprobe_sleepable, BPF_F_SLEEPABLE),
 		       "kprobe_sleepable_flags"))
 		goto cleanup;
 
tools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c +1 -1
index 652307a1b22e..902a70d5afeb 100644
@@ -35,7 +35,7 @@ static int check_load(const char *file, enum bpf_prog_type type)
 	}
 
 	bpf_program__set_type(prog, type);
-	bpf_program__set_flags(prog, bpf_program__flags(prog) | testing_prog_flags());
+	bpf_program__add_flags(prog, testing_prog_flags());
 	bpf_program__set_log_level(prog, 4 | extra_prog_load_log_flags);
 
 	err = bpf_object__load(obj);
tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c +1 -2
index ed3fd0a88dab..651123cd8602 100644
@@ -361,8 +361,7 @@ static void test_attach_api_fails(void)
 
 	sl_skel->bss->user_ptr = sl_skel;
 
-	err = bpf_program__set_flags(sl_skel->progs.handle_kprobe_multi_sleepable,
-				     bpf_program__flags(sl_skel->progs.handle_kprobe_multi_sleepable) | BPF_F_SLEEPABLE);
+	err = bpf_program__add_flags(sl_skel->progs.handle_kprobe_multi_sleepable, BPF_F_SLEEPABLE);
 	if (!ASSERT_OK(err, "sleep_skel_set_flags"))
 		goto cleanup;
 
tools/testing/selftests/bpf/prog_tests/xdp_metadata.c +2 -2
index 047dfdc322a2..3ab963458903 100644
@@ -408,14 +408,14 @@ void test_xdp_metadata(void)
 
 	prog = bpf_object__find_program_by_name(bpf_obj->obj, "rx");
 	bpf_program__set_ifindex(prog, rx_ifindex);
-	bpf_program__set_flags(prog, bpf_program__flags(prog) | BPF_F_XDP_DEV_BOUND_ONLY);
+	bpf_program__add_flags(prog, BPF_F_XDP_DEV_BOUND_ONLY);
 
 	/* Make sure we can load a dev-bound program that performs
 	 * XDP_REDIRECT into a devmap.
 	 */
 	new_prog = bpf_object__find_program_by_name(bpf_obj->obj, "redirect");
 	bpf_program__set_ifindex(new_prog, rx_ifindex);
-	bpf_program__set_flags(new_prog, bpf_program__flags(new_prog) | BPF_F_XDP_DEV_BOUND_ONLY);
+	bpf_program__add_flags(new_prog, BPF_F_XDP_DEV_BOUND_ONLY);
 
 	if (!ASSERT_OK(xdp_metadata__load(bpf_obj), "load skeleton"))
 		goto out;
tools/testing/selftests/bpf/test_loader.c +2 -3
index 794a7dfb0579..28724de06322 100644
@@ -748,7 +748,7 @@ static void prepare_case(struct test_loader *tester,
 			 struct bpf_object *obj,
 			 struct bpf_program *prog)
 {
-	int min_log_level = 0, prog_flags;
+	int min_log_level = 0;
 
 	if (env.verbosity > VERBOSE_NONE)
 		min_log_level = 1;
@@ -766,8 +766,7 @@ static void prepare_case(struct test_loader *tester,
 	else
 		bpf_program__set_log_level(prog, spec->log_level);
 
-	prog_flags = bpf_program__flags(prog);
-	bpf_program__set_flags(prog, prog_flags | spec->prog_flags);
+	bpf_program__add_flags(prog, spec->prog_flags);
 
 	tester->log_buf[0] = '\0';
 }
tools/testing/selftests/bpf/testing_helpers.c +1 -3
index 3f037949e978..d1d60451c5bc 100644
@@ -292,7 +292,6 @@ int bpf_prog_test_load(const char *file, enum bpf_prog_type type,
 	);
 	struct bpf_object *obj;
 	struct bpf_program *prog;
-	__u32 flags;
 	int err;
 
 	obj = bpf_object__open_file(file, &opts);
@@ -308,8 +307,7 @@ int bpf_prog_test_load(const char *file, enum bpf_prog_type type,
 	if (type != BPF_PROG_TYPE_UNSPEC && bpf_program__type(prog) != type)
 		bpf_program__set_type(prog, type);
 
-	flags = bpf_program__flags(prog) | testing_prog_flags();
-	bpf_program__set_flags(prog, flags);
+	bpf_program__add_flags(prog, testing_prog_flags());
 
 	err = bpf_object__load(obj);
 	if (err)
tools/testing/selftests/bpf/veristat.c +2 -2
index e70741c6b9b7..9cfc9b4b41c1 100644
@@ -1722,9 +1722,9 @@ static int process_prog(const char *filename, struct bpf_object *obj, struct bpf
 	fixup_obj(obj, prog, base_filename);
 
 	if (env.force_checkpoints)
-		bpf_program__set_flags(prog, bpf_program__flags(prog) | BPF_F_TEST_STATE_FREQ);
+		bpf_program__add_flags(prog, BPF_F_TEST_STATE_FREQ);
 	if (env.force_reg_invariants)
-		bpf_program__set_flags(prog, bpf_program__flags(prog) | BPF_F_TEST_REG_INVARIANTS);
+		bpf_program__add_flags(prog, BPF_F_TEST_REG_INVARIANTS);
 
 	opts.log_buf = buf;
 	opts.log_size = buf_sz;
tools/testing/selftests/bpf/xdp_hw_metadata.c +1 -1
index c5501b3fdf48..ab3f5fc56e86 100644
@@ -845,7 +845,7 @@ int main(int argc, char *argv[])
 
 	prog = bpf_object__find_program_by_name(bpf_obj->obj, "rx");
 	bpf_program__set_ifindex(prog, ifindex);
-	bpf_program__set_flags(prog, bpf_program__flags(prog) | BPF_F_XDP_DEV_BOUND_ONLY);
+	bpf_program__add_flags(prog, BPF_F_XDP_DEV_BOUND_ONLY);
 
 	printf("load bpf program...\n");
 	ret = xdp_hw_metadata__load(bpf_obj);
Signature · 1 line
2.55.0
#4 lore raw

Re: [PATCH bpf-next v2 3/4] selftests/bpf: Adopt bpf_program__add_flags() helper

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Missed replacement of bpf_program__set_flags() in kasan test, contradicting the commit message. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Missed replacement of bpf_program__set_flags() in kasan test, contradicting the commit message. --

commit ccc6f4464177117b4b9c440f9349d856ca5fd357
Author: Toke Høiland-Jørgensen <[email protected]>

selftests/bpf: Adopt bpf_program__add_flags() helper

Adopt the newly added bpf_program__add_flags() helper everywhere the selftests non-destructively modifies program flags (which turns out to be every use of bpf_program__set_flags() in the tests).

Quoted 5 lines

selftests/bpf: Adopt bpf_program__add_flags() helper

Adopt the newly added bpf_program__add_flags() helper everywhere the selftests non-destructively modifies program flags (which turns out to be every use of bpf_program__set_flags() in the tests).

[Severity: Medium]
Does this patch actually replace every use of bpf_program__set_flags() in the tests?

Looking at tools/testing/selftests/bpf/prog_tests/kasan.c, there appear to be two uses of bpf_program__set_flags() remaining in serial_test_kasan():

tools/testing/selftests/bpf/prog_tests/kasan.c:serial_test_kasan() {
        ...
		bpf_program__set_flags(prog, BPF_F_TEST_RND_HI32);
        ...
		bpf_program__set_flags(prog, BPF_F_TEST_RND_HI32);
        ...
}

Should these also be updated to use bpf_program__add_flags() to match the commit message, or was there a specific reason to leave them as destructive flag updates?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Reviewed-by on v2 3/4
#5 lore raw

Re: [PATCH bpf-next v2 3/4] selftests/bpf: Adopt bpf_program__add_flags() helper

In reply to Toke Høiland-Jørgensen
Quoted 6 lines · Toke Høiland-Jørgensen

Adopt the newly added bpf_program__add_flags() helper everywhere the selftests non-destructively modifies program flags (which turns out to be every use of bpf_program__set_flags() in the tests).

‌AI pays excessive attention to wording.

maybe just delete whole parenthetical notes‌ "(which turns out ...)"

#6 lore raw

Re: [PATCH bpf-next v2 3/4] selftests/bpf: Adopt bpf_program__add_flags() helper

Quoted 9 lines · Jiayuan Chen
Quoted 6 lines · Toke Høiland-Jørgensen

Adopt the newly added bpf_program__add_flags() helper everywhere the selftests non-destructively modifies program flags (which turns out to be every use of bpf_program__set_flags() in the tests).

Quoted 6 lines

‌AI pays excessive attention to wording.

maybe just delete whole parenthetical notes‌ "(which turns out ...)"

Well, the two instances the bot is complaining about turns out to have been added after I wrote that commit message; I didn't re-run the 'git grep' after rebasing for v2, so I didn't notice this.

I'm not sure if those two are candidates for conversion as well, TBH. I can respin with your suggested change to the commit message, or just convert those two as well. Or just leave it as-is, I suppose. What do the maintainers think? :)

-Toke

#7 lore raw

Re: [PATCH bpf-next v2 3/4] selftests/bpf: Adopt bpf_program__add_flags() helper

Quoted 19 lines · Toke Høiland-Jørgensen
Quoted 9 lines · Jiayuan Chen
Quoted 6 lines · Toke Høiland-Jørgensen

Adopt the newly added bpf_program__add_flags() helper everywhere the selftests non-destructively modifies program flags (which turns out to be every use of bpf_program__set_flags() in the tests).

Quoted 6 lines

‌AI pays excessive attention to wording.

maybe just delete whole parenthetical notes‌ "(which turns out ...)"

Quoted 9 lines

Well, the two instances the bot is complaining about turns out to have been added after I wrote that commit message; I didn't re-run the 'git grep' after rebasing for v2, so I didn't notice this.

I'm not sure if those two are candidates for conversion as well, TBH. I can respin with your suggested change to the commit message, or just convert those two as well. Or just leave it as-is, I suppose. What do the maintainers think? :)

let's replace, you'll need another revision anyways, thanks!

Quoted 2 lines

-Toke

#8 lore raw

Re: [PATCH bpf-next v2 3/4] selftests/bpf: Adopt bpf_program__add_flags() helper

Quoted 24 lines · Andrii Nakryiko
Quoted 23 lines · Toke Høiland-Jørgensen
Quoted 9 lines · Jiayuan Chen
Quoted 6 lines · Toke Høiland-Jørgensen

Adopt the newly added bpf_program__add_flags() helper everywhere the selftests non-destructively modifies program flags (which turns out to be every use of bpf_program__set_flags() in the tests).

Quoted 6 lines

‌AI pays excessive attention to wording.

maybe just delete whole parenthetical notes‌ "(which turns out ...)"

Well, the two instances the bot is complaining about turns out to have been added after I wrote that commit message; I didn't re-run the 'git grep' after rebasing for v2, so I didn't notice this.

Quoted 7 lines
Quoted 5 lines

I'm not sure if those two are candidates for conversion as well, TBH. I can respin with your suggested change to the commit message, or just convert those two as well. Or just leave it as-is, I suppose. What do the maintainers think? :)

let's replace, you'll need another revision anyways, thanks!

ACK, will do :)

-Toke

#9 lore raw

PATCH v2 4/4 bpftool: Adopt bpf_program__add_flags() helper

In reply to Toke Høiland-Jørgensen

Adopt the newly added bpf_program__add_flags() helper to non-destructively add the BPF_F_XDP_DEV_BOUND_ONLY flag to XDP programs.

---
 tools/bpf/bpftool/prog.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/bpf/bpftool/prog.c +1 -1
index 8c2f9255b36d..6ab911e82155 100644
@@ -1769,7 +1769,7 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
 		}
 
 		if (prog_type == BPF_PROG_TYPE_XDP && xdpmeta_ifindex) {
-			bpf_program__set_flags(pos, bpf_program__flags(pos) | BPF_F_XDP_DEV_BOUND_ONLY);
+			bpf_program__add_flags(pos, BPF_F_XDP_DEV_BOUND_ONLY);
 			bpf_program__set_ifindex(pos, xdpmeta_ifindex);
 		} else {
 			bpf_program__set_ifindex(pos, offload_ifindex);
Signature · 1 line
2.55.0
#10 lore raw
In reply to Toke Høiland-Jørgensen
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The newly introduced multi-line comment in `libbpf.h` does not follow the required BPF subsystem…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The newly introduced multi-line comment in libbpf.h does not follow the required BPF subsystem comment style. --

commit 70c14f22413bc6ac7381ed8252533a749bda07d7
Author: Toke Høiland-Jørgensen <[email protected]>

libbpf: Add bpf_program__add_flags() and bpf_program__clear_flags()

This commit introduces two convenience helpers, bpf_program__add_flags() and bpf_program__clear_flags(), which wrap bpf_program__set_flags(). They allow modifying BPF program flags without overwriting existing ones.

[ ... ]

tools/lib/bpf/libbpf.h bpf_program__set_expected_attach_type(struct bpf_program *prog, · line 1016

Quoted 11 lines
tools/lib/bpf/libbpf.h +3 -0
index b965ad5715403..3932bf9cb4903 100644
@@ -1011,6 +1011,12 @@ bpf_program__set_expected_attach_type(struct bpf_program *prog,
 LIBBPF_API __u32 bpf_program__flags(const struct bpf_program *prog);
 LIBBPF_API int bpf_program__set_flags(struct bpf_program *prog, __u32 flags);
 
+/* Convenience helpers to non-destructively add or clear the specified flags
+ * instead of clobbering them as bpf_program__set_flags() does.
+ */

[Severity: Low]
This isn't a bug, but does this multi-line comment format violate the BPF subsystem coding style?

The BPF guidelines explicitly require multi-line comments to have the opening /* on its own line, rather than having text on the same line as the opening.

Quoted 2 lines
tools/lib/bpf/libbpf.h +2 -0
+LIBBPF_API int bpf_program__add_flags(struct bpf_program *prog, __u32 flags);
+LIBBPF_API int bpf_program__clear_flags(struct bpf_program *prog, __u32 flags);

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Acked-by on v2 1/4
#11 lore raw
In reply to Toke Høiland-Jørgensen
Quoted 11 lines · Toke Høiland-Jørgensen

When changing BPF program flags, applications often need to just add or remove a single flag, without touching the existing ones. When using the bpf_program__set_flags() function, this requires reading the existing flags and doing bitwise manipulations before resetting the result, which is slightly awkward to do, and easy to forget.

Add two new convenience helpers, bpf_program__add_flags() and bpf_program__clear_flags(), which wraps bpf_program__set_flags() in the bitwise operations required to modify flags without clobbering the existing ones.

Quoted 4 lines

---

...

Reviewed-by on v2 1/4
#12 lore raw
In reply to Toke Høiland-Jørgensen
Quoted 6 lines · Toke Høiland-Jørgensen

When changing BPF program flags, applications often need to just add or remove a single flag, without touching the existing ones. When using the bpf_program__set_flags() function, this requires reading the existing flags and doing bitwise manipulations before resetting the result, which is slightly awkward to do, and easy to forget.

Quoted 8 lines

Add two new convenience helpers, bpf_program__add_flags() and bpf_program__clear_flags(), which wraps bpf_program__set_flags() in the bitwise operations required to modify flags without clobbering the existing ones.

#13 lore raw
In reply to Toke Høiland-Jørgensen
Quoted 47 lines · Toke Høiland-Jørgensen

When changing BPF program flags, applications often need to just add or remove a single flag, without touching the existing ones. When using the bpf_program__set_flags() function, this requires reading the existing flags and doing bitwise manipulations before resetting the result, which is slightly awkward to do, and easy to forget.

Add two new convenience helpers, bpf_program__add_flags() and bpf_program__clear_flags(), which wraps bpf_program__set_flags() in the bitwise operations required to modify flags without clobbering the existing ones.

---
 tools/lib/bpf/libbpf.c   | 10 ++++++++++
 tools/lib/bpf/libbpf.h   |  6 ++++++
 tools/lib/bpf/libbpf.map |  2 ++
 3 files changed, 18 insertions(+)
tools/lib/bpf/libbpf.c +10 -0
index c036e8a91ed8..221c4fe1fb31 100644
@@ -9938,6 +9938,16 @@ int bpf_program__set_flags(struct bpf_program *prog, __u32 flags)
        return 0;
 }
 
+int bpf_program__add_flags(struct bpf_program *prog, __u32 flags)
+{
+       return bpf_program__set_flags(prog, prog->prog_flags | flags);
+}
+
+int bpf_program__clear_flags(struct bpf_program *prog, __u32 flags)
+{
+       return bpf_program__set_flags(prog, prog->prog_flags & ~flags);
+}
+
 __u32 bpf_program__log_level(const struct bpf_program *prog)
 {
        return prog->log_level;
tools/lib/bpf/libbpf.h +0 -0
index b965ad571540..3932bf9cb490 100644

tools/lib/bpf/libbpf.h bpf_program__set_expected_attach_type(struct bpf_program *prog, · line 1016

Quoted 6 lines
tools/lib/bpf/libbpf.h +3 -0
@@ -1011,6 +1011,12 @@ bpf_program__set_expected_attach_type(struct bpf_program *prog,
 LIBBPF_API __u32 bpf_program__flags(const struct bpf_program *prog);
 LIBBPF_API int bpf_program__set_flags(struct bpf_program *prog, __u32 flags);
 
+/* Convenience helpers to non-destructively add or clear the specified flags
+ * instead of clobbering them as bpf_program__set_flags() does.
+ */

I don't think this is a correct format for doc comment, please follow the pattern established by other API comments

pw-bot: cr

Quoted 24 lines
tools/lib/bpf/libbpf.h +3 -0
+LIBBPF_API int bpf_program__add_flags(struct bpf_program *prog, __u32 flags);
+LIBBPF_API int bpf_program__clear_flags(struct bpf_program *prog, __u32 flags);
+
 
 /* Per-program log level and log buffer getters/setters.
  * See bpf_object_open_opts comments regarding log_level and log_buf

* interactions.

tools/lib/bpf/libbpf.map +2 -1
index 08ab2ea881fb..a811a1b3a085 100644
@@ -458,7 +458,9 @@ LIBBPF_1.7.0 {
 
 LIBBPF_1.8.0 {
        global:
+               bpf_program__add_flags;
                bpf_program__attach_tracing_multi;
+               bpf_program__clear_flags;
                bpf_program__clone;
                btf__find_by_name_kind_own;
                btf__new_empty_opts;
--

2.55.0