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 2026-09-12 10:41:00 +02:00 committed by Andrii Nakryiko
parent 29fcf9ce4b
commit 3041e498f9
3 changed files with 34 additions and 0 deletions

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;

View File

@ -1011,6 +1011,28 @@ 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);
/**
* @brief **bpf_program__add_flags()** adds one or more flags to the BPF
* program, preserving any existing flags.
*
* @param prog BPF program
* @param flags the flags to add
*
* @return 0, on success; negative error code, otherwise
*/
LIBBPF_API int bpf_program__add_flags(struct bpf_program *prog, __u32 flags);
/**
* @brief **bpf_program__clear_flags()** clears one or more flags from the BPF
* program, preserving any other flags not explicitly cleared.
*
* @param prog BPF program
* @param flags the flags to clear
*
* @return 0, on success; negative error code, otherwise
*/
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.

View File

@ -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;