Skip to content

bpf: Extend BPF syscall with common attributes support#11008

Closed
Asphaltt wants to merge 8 commits intokernel-patches:bpf-next_basefrom
Asphaltt:bpf/extend-bpf-syscall/v10
Closed

bpf: Extend BPF syscall with common attributes support#11008
Asphaltt wants to merge 8 commits intokernel-patches:bpf-next_basefrom
Asphaltt:bpf/extend-bpf-syscall/v10

Conversation

@Asphaltt
Copy link
Contributor

No description provided.

Extend the BPF syscall to support a set of common attributes shared
across all BPF commands:

1. 'log_buf': User-provided buffer for storing logs.
2. 'log_size': Size of the log buffer.
3. 'log_level': Log verbosity level.
4. 'log_true_size': The size of log reported by kernel.

These common attributes are passed as the 4th argument to the BPF
syscall, with the 5th argument specifying the size of this structure.

To indicate the use of these common attributes from userspace, a new flag
'BPF_COMMON_ATTRS' ('1 << 16') is introduced. This flag is OR-ed into the
'cmd' field of the syscall.

When 'cmd & BPF_COMMON_ATTRS' is set, the kernel will copy the common
attributes from userspace into kernel space for use.

Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
To support the extended BPF syscall introduced in the previous commit,
introduce the following internal APIs:

* 'sys_bpf_ext()'
* 'sys_bpf_ext_fd()'
  They wrap the raw 'syscall()' interface to support passing extended
  attributes.
* 'probe_sys_bpf_ext()'
  Check whether current kernel supports the BPF syscall common attributes.

Acked-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
The next commit will add support for reporting logs via extended common
attributes, including 'log_true_size'.

To prepare for that, refactor the 'log_true_size' reporting logic by
introducing a new struct bpf_log_attr to encapsulate log-related behavior:

 * bpf_prog_load_log_attr_init(): initialize the log fields, which will
   support extended common attributes in the next commit.
 * bpf_log_attr_finalize(): handle log finalization and write back
   'log_true_size' to userspace.

Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
BPF_PROG_LOAD can now provide log parameters through both union bpf_attr
and struct bpf_common_attr. Define clear conflict and precedence rules:

- if both are provided and log_buf/log_size/log_level match, use them;
- if only one side provides the log attributes, use that one;
- if both provide log buffers but differ, return -EINVAL.

Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
By extending syscall common attributes to BPF_BTF_LOAD, log parameters
can now be supplied through either union bpf_attr or struct
bpf_common_attr.

Apply the same conflict detection and precedence rules used by prog_load:

- if both are provided and btf_log_buf/btf_log_size/btf_log_level match,
  use them;
- if only one side provides the log attributes, use that one;
- if both provide log buffers but differ, return -EINVAL.

With log handling centralized, drop btf.c::finalize_log() and finalize BTF
verifier logs via bpf_log_attr_finalize() instead.

Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
Many BPF_MAP_CREATE validation failures currently return -EINVAL without
any explanation to userspace.

Plumb common syscall log attributes into map_create(), create a verifier
log from bpf_common_attr::log_buf/log_size/log_level, and report
map-creation failure reasons through that buffer.

This improves debuggability by allowing userspace to inspect why map
creation failed and read back log_true_size from common attributes.

Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
With the previous commit adding common attribute support for
BPF_MAP_CREATE, users can now retrieve detailed error messages when map
creation fails via the log_buf field.

Introduce struct bpf_log_opts with the following fields:
log_buf, log_size, log_level, and log_true_size.

Extend bpf_map_create_opts with a new field log_opts, allowing users to
capture and inspect log messages on map creation failures.

Acked-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
Add tests to verify that the kernel reports the expected error messages
when map creation fails.

Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
@Asphaltt Asphaltt closed this Feb 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant