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.

Suggested-by: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Reviewed-by: Jiayuan Chen <jiayuan.chen@linux.dev>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Acked-by: Ihor Solodrai <ihor.solodrai@linux.dev>
Link: https://lore.kernel.org/bpf/20260912084109.432834-1-toke@redhat.com
This commit is contained in:
Toke Høiland-Jørgensen authored and Andrii Nakryiko committed 2026-10-02 05:41:50 -07:00
1 parent fa3579b3e8
commit 0f9c5a8d04
3 files changed
+34

No files matched your search

+10
View File
@@ -9934,6 +9934,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;