Skip to content

bpf: Extend BPF syscall with common attributes support

Leon Hwang bpf linux-api linux-kselftest lkml sashiko-reviews 16 versions – Applied

Status from this message: This series was applied to bpf/bpf-next.git (master)

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v14 Latest8 patches 5 repliesApplied 8/8
  2. v138 patches 12 repliesSuperseded
  3. v128 patches 2 repliesSuperseded
Earlier versions · 13
VersionPostedPatchesRepliesStatus
v118 patches0 repliesSuperseded
v108 patches10 repliesSuperseded
v99 patches13 repliesSuperseded
v89 patches2 repliesSuperseded
v79 patches6 repliesSuperseded
v6 posting 2/29 patches3 repliesSuperseded
v6 posting 1/26 of 9 patches0 repliesSuperseded
v59 patches9 repliesSuperseded
v4 posting 2/29 patches5 repliesSuperseded
v4 posting 1/25 of 9 patches0 repliesSuperseded
v3 RFC10 patches11 repliesSuperseded
v2 RFC6 patches18 repliesSuperseded
v1 RFC5 patches3 repliesSuperseded

Changes in v14 from the cover letter

* Replace __u64 with __aligned_u64 in struct bpf_common_attr in patch #1 (per bot+bpf-ci).
* Add a single line comment for preserving original error in patch #6 (per bot+bpf-ci and Alexei).
* Drop unused label in patch #6 (per bot+bpf-ci).
* v13: https://lore.kernel.org/bpf/[email protected]/

Patches in v14 All 8 applied

#Subject and filesSizeRepliesApplied
1/8bpf: Extend BPF syscall with common attributes supportinclude/linux/syscalls.h include/uapi/linux/bpf.h kernel/bpf/syscall.c +1 more+39 -51 replyApplied
2/8libbpf: Add support for extended BPF syscalltools/lib/bpf/bpf.c tools/lib/bpf/features.c tools/lib/bpf/libbpf_internal.h+47 -01 replyApplied
3/8bpf: Refactor reporting log_true_size for prog_loadinclude/linux/bpf.h include/linux/bpf_verifier.h kernel/bpf/log.c +2 more+57 -170 repliesApplied
4/8bpf: Add syscall common attributes support for prog_loadinclude/linux/bpf_verifier.h kernel/bpf/log.c kernel/bpf/syscall.c+31 -90 repliesApplied
5/8bpf: Add syscall common attributes support for btf_loadinclude/linux/btf.h kernel/bpf/btf.c kernel/bpf/syscall.c+17 -270 repliesApplied
6/8bpf: Add syscall common attributes support for map_createinclude/linux/bpf_verifier.h kernel/bpf/log.c kernel/bpf/syscall.c+88 -101 replyApplied
7/8libbpf: Add syscall common attributes support for map_createtools/lib/bpf/bpf.c tools/lib/bpf/bpf.h+31 -21 replyApplied
8/8selftests/bpf: Add tests to verify map create failure logtools/testing/selftests/bpf/prog_tests/map_init.c+166 -01 replyApplied

Reviews trailers per patch

4 of 8 patches have a Reviewed-by or Acked-by.

PatchAndrii Nakryiko
1/8
2/8A-b
3/8A-b
4/8
5/8A-b
6/8
7/8A-b
8/8

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v14 thread

Cover letter [PATCH bpf-next v14 0/8] bpf: Extend BPF syscall with common attributes support

This patch series builds upon the discussion in
"[PATCH bpf-next v4 0/4] bpf: Improve error reporting for freplace attachment failure" [1].

This patch series introduces support for *common attributes* in the BPF syscall, providing a unified mechanism for passing shared metadata across all BPF commands, initially used by BPF_PROG_LOAD, BPF_BTF_LOAD, and BPF_MAP_CREATE.

The initial set of common attributes includes:

1. 'log_buf': User-provided buffer for storing log output.
2. 'log_size': Size of the provided log buffer.
3. 'log_level': Verbosity level for logging.
4. 'log_true_size': Actual log size reported by kernel.

With this extension, the BPF syscall will be able to return meaningful error messages (e.g., map creation failures), improving debuggability and user experience.

Links:
[1] https://lore.kernel.org/bpf/[email protected]/

Changes:
v13 -> v14:
* Replace __u64 with __aligned_u64 in struct bpf_common_attr in patch #1 (per bot+bpf-ci).
* Add a single line comment for preserving original error in patch #6 (per bot+bpf-ci and Alexei).
* Drop unused label in patch #6 (per bot+bpf-ci).
* v13: https://lore.kernel.org/bpf/[email protected]/

v12 -> v13:
* Rebase on bpf-next tree to resolve code conflict.
* Report log_true_size on success path in patch #6.
* Check size instead of common->log_buf in patch #6 (per bot+bpf-ci).
* v12: https://lore.kernel.org/bpf/[email protected]/

v11 -> v12:
* Drop "log_" prefix in struct bpf_log_attr in patch #3.
* Drop "log_" prefix in struct bpf_log_opts in patch #7.
* Copy log_true_size using copy_to_bpfptr_offset() in patch #3 (per Alexei).
* v11: https://lore.kernel.org/bpf/[email protected]/

v10 -> v11:
* Collect Acked-by from Andrii, thanks.
* Validate whether log_buf, log_size, and log_level are valid by reusing bpf_verifier_log_attr_valid() in patch #4 (per Andrii).
* v10: https://lore.kernel.org/bpf/[email protected]/

v9 -> v10:
* Collect Acked-by from Andrii, thanks.
* Address comments from Andrii:
* Drop log NULL check in bpf_log_attr_finalize().
* Return -EFAULT early in bpf_log_attr_finalize().
* Validate whether log_buf, log_size, and log_level are set.
* Keep log_buf, log_size, log_level, and user-pointer log_true_size in struct bpf_log_attr.
* Make prog_load and btf_load work with the new struct bpf_log_attr.
* Add comment to log_true_size of struct bpf_log_opts in libbpf.
* Address comment from Alexei:
* Avoid using BPF_LOG_FIXED as log_level in tests.
* v9: https://lore.kernel.org/bpf/[email protected]/

v8 -> v9:
* Rework reporting 'log_true_size' for prog_load, btf_load, and map_create to simplify struct bpf_log_attr (per Alexei).
* v8: https://lore.kernel.org/bpf/[email protected]/

v7 -> v8:
* Return 0 when fd < 0 and errno != EFAULT in probe_sys_bpf_ext(), then simplify probe_bpf_syscall_common_attrs() (per Alexei and Andrii).
* v7: https://lore.kernel.org/bpf/[email protected]/

v6 -> v7:
* Return -errno when fd < 0 and errno != EFAULT in probe_sys_bpf_ext().
* Convert return value of probe_sys_bpf_ext() to bool in probe_bpf_syscall_common_attrs().
* Address comments from Andrii:
* Drop the comment, and handle fd >= 0 case explicitly in

    probe_sys_bpf_ext().

* Return an error when fd >= 0 in probe_sys_bpf_ext().
* v6: https://lore.kernel.org/bpf/[email protected]/

v5 -> v6:
* Address comments from Andrii:
* Update some variables' name.
* Drop unnecessary 'close(fd)' in libbpf.
* Rename FEAT_EXTENDED_SYSCALL to FEAT_BPF_SYSCALL_COMMON_ATTRS with updated description in libbpf.
* Use EINVAL instead of EUSERS, as EUSERS is not used in bpf yet.
* Rename struct bpf_syscall_common_attr_opts to bpf_log_opts in libbpf.
* Add 'OPTS_SET(log_opts, log_true_size, 0);' in libbpf's 'bpf_map_create()'.
* v5: https://lore.kernel.org/bpf/[email protected]/

v4 -> v5:
* Rework reporting 'log_true_size' for prog_load, btf_load, and map_create (per Alexei).
* v4: https://lore.kernel.org/bpf/[email protected]/

RFC v3 -> v4:
* Drop RFC.
* Address comments from Andrii:
* Add parentheses in 'sys_bpf_ext()'.
* Avoid creating new fd in 'probe_sys_bpf_ext()'.
* Add a new struct to wrap log fields in libbpf.
* Address comments from Alexei:
* Do not skip writing to user space when log_true_size is zero.
* Do not use 'bool' arguments.
* Drop the adding WARN_ON_ONCE()'s.
* v3: https://lore.kernel.org/bpf/[email protected]/

RFC v2 -> RFC v3:
* Rename probe_sys_bpf_extended to probe_sys_bpf_ext.
* Refactor reporting 'log_true_size' for prog_load.
* Refactor reporting 'btf_log_true_size' for btf_load.
* Add warnings for internal bugs in map_create.
* Check log_true_size in test cases.
* Address comment from Alexei:
* Change kvzalloc/kvfree to kzalloc/kfree.
* Address comments from Andrii:
* Move BPF_COMMON_ATTRS to 'enum bpf_cmd' alongside brief comment.
* Add bpf_check_uarg_tail_zero() for extra checks.
* Rename sys_bpf_extended to sys_bpf_ext.
* Rename sys_bpf_fd_extended to sys_bpf_ext_fd.
* Probe the new feature using NULL and -EFAULT.
* Move probe_sys_bpf_ext to libbpf_internal.h and drop LIBBPF_API.
* Return -EUSERS when log attrs are conflict between bpf_attr and bpf_common_attr.
* Avoid touching bpf_vlog_init().
* Update the reason messages in map_create.
* Finalize the log using __cleanup().
* Report log size to users.
* Change type of log_buf from '__u64' to 'const char *' and cast type

    using ptr_to_u64() in bpf_map_create().

* Do not return -EOPNOTSUPP when kernel doesn't support this feature in bpf_map_create().
* Add log_level support for map creation for consistency.
* Address comment from Eduard:
* Use common_attrs->log_level instead of BPF_LOG_FIXED.
* v2: https://lore.kernel.org/bpf/[email protected]/

RFC v1 -> RFC v2:
* Fix build error reported by test bot.
* Address comments from Alexei:
* Drop new uapi for freplace.
* Add common attributes support for prog_load and btf_load.
* Add common attributes support for map_create.
* v1: https://lore.kernel.org/bpf/[email protected]/

Leon Hwang (8):
bpf: Extend BPF syscall with common attributes support
libbpf: Add support for extended BPF syscall
bpf: Refactor reporting log_true_size for prog_load
bpf: Add syscall common attributes support for prog_load
bpf: Add syscall common attributes support for btf_load
bpf: Add syscall common attributes support for map_create
libbpf: Add syscall common attributes support for map_create selftests/bpf: Add tests to verify map create failure log

 include/linux/bpf.h                           |   4 +-
 include/linux/bpf_verifier.h                  |  16 ++
 include/linux/btf.h                           |   3 +-
 include/linux/syscalls.h                      |   3 +-
 include/uapi/linux/bpf.h                      |   8 +
 kernel/bpf/btf.c                              |  30 +---
 kernel/bpf/log.c                              |  90 +++++++++-
 kernel/bpf/syscall.c                          | 115 +++++++++---
 kernel/bpf/verifier.c                         |  17 +-
 tools/include/uapi/linux/bpf.h                |   8 +
 tools/lib/bpf/bpf.c                           |  52 +++++-
 tools/lib/bpf/bpf.h                           |  17 +-
 tools/lib/bpf/features.c                      |   8 +
 tools/lib/bpf/libbpf_internal.h               |   3 +
 .../selftests/bpf/prog_tests/map_init.c       | 166 ++++++++++++++++++
 15 files changed, 473 insertions(+), 67 deletions(-)

--
2.54.0