bpf: Extend BPF syscall with common attributes support
Subsystems (6)
v13 cover 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:
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
PATCH v13 1/8 bpf: Extend BPF syscall with common attributes support
Add generic BPF syscall support for passing common attributes.
The initial set of common attributes includes:
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': Actual log size reported by kernel.
The common-attribute pointer and its size are passed as the 4th and 5th syscall arguments. A new command bit, 'BPF_COMMON_ATTRS' ('1 << 16'), indicates that common attributes are supplied.
This commit adds syscall and uapi plumbing. Command-specific handling is added in follow-up patches.
--- include/linux/syscalls.h | 3 ++- include/uapi/linux/bpf.h | 8 ++++++++ kernel/bpf/syscall.c | 25 +++++++++++++++++++++---- tools/include/uapi/linux/bpf.h | 8 ++++++++ 4 files changed, 39 insertions(+), 5 deletions(-)
include/linux/syscalls.h +2 -1
@@ -936,7 +936,8 @@ asmlinkage long sys_seccomp(unsigned int op, unsigned int flags,asmlinkage long sys_getrandom (char __user *buf,size_t count,unsigned int flags);asmlinkage long sys_memfd_create (const char __user *uname_ptr,unsigned int flags); -asmlinkage long sys_bpf (int cmd,union bpf_attr __user *attr,unsigned int size); +asmlinkage long sys_bpf (int cmd,union bpf_attr __user *attr,unsigned int size, +struct bpf_common_attr __user *attr_common,unsigned int size_common);asmlinkage long sys_execveat (int dfd,const char __user *filename,const char __user *const __user *argv,const char __user *const __user *envp,int flags);
include/uapi/linux/bpf.h +8 -0
@@ -994,6 +994,7 @@ enum bpf_cmd {BPF_PROG_STREAM_READ_BY_FD ,BPF_PROG_ASSOC_STRUCT_OPS , __MAX_BPF_CMD, + BPF_COMMON_ATTRS = 1 << 16, /* Indicate carrying syscall common attrs. */ }; enum bpf_map_type {
@@ -1500,6 +1501,13 @@ struct bpf_stack_build_id { }; }; +struct bpf_common_attr { +__u64 log_buf; +__u32 log_size; +__u32 log_level; +__u32 log_true_size; +}; +#define BPF_OBJ_NAME_LEN 16Uenum {
kernel/bpf/syscall.c +21 -4
@@ -6211,8 +6211,10 @@ static int prog_assoc_struct_ops(union bpf_attr *attr)return ret; } -static int __sys_bpf (enum bpf_cmd cmd,bpfptr_t uattr,unsigned int size) +static int __sys_bpf (enum bpf_cmd cmd,bpfptr_t uattr,unsigned int size, +bpfptr_t uattr_common,unsigned int size_common) { +struct bpf_common_attr attr_common;union bpf_attr attr;int err;
@@ -6226,6 +6228,20 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size)if (copy_from_bpfptr (&attr, uattr, size) !=0 )return -EFAULT ; +memset (&attr_common,0 ,sizeof (attr_common)); +if (cmd &BPF_COMMON_ATTRS ) { + err =bpf_check_uarg_tail_zero (uattr_common,sizeof (attr_common), size_common); +if (err) +return err; + + cmd &= ~BPF_COMMON_ATTRS ; + size_common =min_t (u32, size_common,sizeof (attr_common)); +if (copy_from_bpfptr (&attr_common, uattr_common, size_common) !=0 ) +return -EFAULT ; + }else { + size_common =0 ; + } + err =security_bpf (cmd, &attr, size, uattr.is_kernel);if (err <0 )return err;
@@ -6361,9 +6377,10 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size)return err; } -SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size) +SYSCALL_DEFINE5(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size, + struct bpf_common_attr __user *, uattr_common, unsigned int, size_common) { -return __sys_bpf (cmd,USER_BPFPTR (uattr), size); +return __sys_bpf (cmd,USER_BPFPTR (uattr), size,USER_BPFPTR (uattr_common), size_common); } static bool syscall_prog_is_valid_access(int off, int size,
@@ -6393,7 +6410,7 @@ BPF_CALL_3(bpf_sys_bpf, int, cmd, union bpf_attr *, attr, u32, attr_size)default :return -EINVAL ; } -return __sys_bpf (cmd,KERNEL_BPFPTR (attr), attr_size); +return __sys_bpf (cmd,KERNEL_BPFPTR (attr), attr_size,KERNEL_BPFPTR (NULL ),0 ); }
tools/include/uapi/linux/bpf.h +8 -0
@@ -994,6 +994,7 @@ enum bpf_cmd {BPF_PROG_STREAM_READ_BY_FD ,BPF_PROG_ASSOC_STRUCT_OPS , __MAX_BPF_CMD, + BPF_COMMON_ATTRS = 1 << 16, /* Indicate carrying syscall common attrs. */ }; enum bpf_map_type {
@@ -1500,6 +1501,13 @@ struct bpf_stack_build_id { }; }; +struct bpf_common_attr { +__u64 log_buf; +__u32 log_size; +__u32 log_level; +__u32 log_true_size; +}; +#define BPF_OBJ_NAME_LEN 16Uenum {
Signature · 1 line
2.54.0
[email protected]
AI reviewShould log_buf use __aligned_u64 instead of plain __u64?
Quoted 14 lines
include/linux/syscalls.h +2
-1@@ -936,7 +936,8 @@ asmlinkage long sys_seccomp(unsigned int op, unsigned int flags,asmlinkage long sys_getrandom (char __user *buf,size_t count,unsigned int flags);asmlinkage long sys_memfd_create (const char __user *uname_ptr,unsigned int flags); -asmlinkage long sys_bpf (int cmd,union bpf_attr __user *attr,unsigned int size); +asmlinkage long sys_bpf (int cmd,union bpf_attr __user *attr,unsigned int size, +struct bpf_common_attr __user *attr_common,unsigned int size_common);asmlinkage long sys_execveat (int dfd,const char __user *filename,const char __user *const __user *argv,const char __user *const __user *envp,int flags);
[ ... ]
Quoted 10 lines
tools/include/uapi/linux/bpf.h +1
-0@@ -994,6 +994,7 @@ enum bpf_cmd {BPF_PROG_STREAM_READ_BY_FD ,BPF_PROG_ASSOC_STRUCT_OPS , __MAX_BPF_CMD, + BPF_COMMON_ATTRS = 1 << 16, /* Indicate carrying syscall common attrs. */ };
[ ... ]
tools/include/uapi/linux/bpf.h struct bpf_stack_build_id { · line 1510
Quoted 11 lines
tools/include/uapi/linux/bpf.h +7
-0@@ -1500,6 +1501,13 @@ struct bpf_stack_build_id { }; }; +struct bpf_common_attr { +__u64 log_buf; +__u32 log_size; +__u32 log_level; +__u32 log_true_size; +}; +
Should log_buf use __aligned_u64 instead of plain __u64?
Looking at other pointer-bearing fields in the BPF UAPI, they consistently use __aligned_u64. For example, the existing BPF_PROG_LOAD log_buf in union bpf_attr is declared as:
__aligned_u64 log_buf; /* user supplied buffer */
The __aligned_u64 attribute forces 8-byte alignment on all architectures, including 32-bit systems where native unsigned long long alignment is only 4 bytes. Without this attribute, struct bpf_common_attr will have different layouts between 32-bit and 64-bit ABIs:
- 32-bit: 20 bytes, 4-byte aligned
- 64-bit: 24 bytes (4 bytes of tail padding), 8-byte aligned
While the current __sys_bpf() implementation with bpf_check_uarg_tail_zero() and min_t() capped copying handles this gracefully, deviating from the established BPF UAPI convention could create problems for future extensions. If another __u64 field is added later, the inter-field offsets would diverge between ABIs.
Would it be better to use __aligned_u64 log_buf to match union bpf_attr and preserve identical struct layouts across 32/64-bit ABIs?
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/25680759838
Quoted 58 lines · bot+bpf-ci
Quoted 14 lines
include/linux/syscalls.h +2
-1@@ -936,7 +936,8 @@ asmlinkage long sys_seccomp(unsigned int op, unsigned int flags,asmlinkage long sys_getrandom (char __user *buf,size_t count,unsigned int flags);asmlinkage long sys_memfd_create (const char __user *uname_ptr,unsigned int flags); -asmlinkage long sys_bpf (int cmd,union bpf_attr __user *attr,unsigned int size); +asmlinkage long sys_bpf (int cmd,union bpf_attr __user *attr,unsigned int size, +struct bpf_common_attr __user *attr_common,unsigned int size_common);asmlinkage long sys_execveat (int dfd,const char __user *filename,const char __user *const __user *argv,const char __user *const __user *envp,int flags);[ ... ]
Quoted 10 lines
tools/include/uapi/linux/bpf.h +1
-0@@ -994,6 +994,7 @@ enum bpf_cmd {BPF_PROG_STREAM_READ_BY_FD ,BPF_PROG_ASSOC_STRUCT_OPS , __MAX_BPF_CMD, + BPF_COMMON_ATTRS = 1 << 16, /* Indicate carrying syscall common attrs. */ };[ ... ]
tools/include/uapi/linux/bpf.h struct bpf_stack_build_id { · line 1510
Quoted 11 lines
tools/include/uapi/linux/bpf.h +7
-0@@ -1500,6 +1501,13 @@ struct bpf_stack_build_id { }; }; +struct bpf_common_attr { +__u64 log_buf; +__u32 log_size; +__u32 log_level; +__u32 log_true_size; +}; +Should
log_bufuse__aligned_u64instead of plain__u64?Looking at other pointer-bearing fields in the BPF UAPI, they consistently use
__aligned_u64. For example, the existingBPF_PROG_LOADlog_bufin unionbpf_attris declared as:
__aligned_u64log_buf; /* user supplied buffer */The
__aligned_u64attribute forces 8-byte alignment on all architectures, including 32-bit systems where native unsigned long long alignment is only 4 bytes. Without this attribute, structbpf_common_attrwill have different layouts between 32-bit and 64-bit ABIs:- 32-bit: 20 bytes, 4-byte aligned
- 64-bit: 24 bytes (4 bytes of tail padding), 8-byte aligned
Quoted 8 lines
While the current
__sys_bpf()implementation withbpf_check_uarg_tail_zero()andmin_t()capped copying handles this gracefully, deviating from the established BPF UAPI convention could create problems for future extensions. If another__u64field is added later, the inter-field offsets would diverge between ABIs.Would it be better to use
__aligned_u64log_bufto match unionbpf_attrand preserve identical struct layouts across 32/64-bit ABIs?
bot has a point.
pw-bot: cr
Quoted 1 line · Alexei Starovoitov
On Mon May 11, 2026 at 9:19 AM PDT, bot+bpf-ci wrote:
[...]
Quoted 6 lines
Quoted 3 lines
Would it be better to use
__aligned_u64log_bufto match unionbpf_attrand preserve identical struct layouts across 32/64-bit ABIs?bot has a point.
Will fix.
Thanks,
Leon
PATCH v13 2/8 libbpf: Add support for extended BPF syscall
In reply to Leon HwangTo 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.
--- tools/lib/bpf/bpf.c | 36 +++++++++++++++++++++++++++++++++ tools/lib/bpf/features.c | 8 ++++++++ tools/lib/bpf/libbpf_internal.h | 3 +++ 3 files changed, 47 insertions(+)
tools/lib/bpf/bpf.c +36 -0
@@ -69,6 +69,42 @@ static inline __u64 ptr_to_u64(const void *ptr)return (__u64 ) (unsigned long ) ptr; } +static inline int sys_bpf_ext (enum bpf_cmd cmd,union bpf_attr *attr, +unsigned int size, +struct bpf_common_attr *attr_common, +unsigned int size_common) +{ + cmd = attr_common ? (cmd |BPF_COMMON_ATTRS ) : (cmd & ~BPF_COMMON_ATTRS ); +return syscall (__NR_bpf, cmd, attr, size, attr_common, size_common); +} + +static inline int sys_bpf_ext_fd (enum bpf_cmd cmd,union bpf_attr *attr, +unsigned int size, +struct bpf_common_attr *attr_common, +unsigned int size_common) +{ +int fd; + + fd =sys_bpf_ext (cmd, attr, size, attr_common, size_common); +return ensure_good_fd (fd); +} + +int probe_sys_bpf_ext (void ) +{ + const size_t attr_sz = offsetofend(union bpf_attr, prog_token_fd); +union bpf_attr attr; +int fd; + +memset (&attr,0 , attr_sz); + fd =syscall (__NR_bpf,BPF_PROG_LOAD |BPF_COMMON_ATTRS , &attr, attr_sz,NULL , +sizeof (struct bpf_common_attr )); +if (fd >=0 ) { +close (fd); +return -EINVAL ; + } +return errno ==EFAULT ?1 :0 ; +} +static inline int sys_bpf (enum bpf_cmd cmd,union bpf_attr *attr,unsigned int size) {
tools/lib/bpf/features.c +8 -0
@@ -615,6 +615,11 @@ static int probe_kern_btf_layout(int token_fd) (char *)layout, token_fd)); } +static int probe_bpf_syscall_common_attrs (int token_fd) +{ +return probe_sys_bpf_ext (); +} +typedef int (*feature_probe_fn )(int /* token_fd */ );static struct kern_feature_cache feature_cache;
@@ -699,6 +704,9 @@ static struct kern_feature_desc { [FEAT_BTF_LAYOUT ] = {"kernel supports BTF layout" , probe_kern_btf_layout, }, + [FEAT_BPF_SYSCALL_COMMON_ATTRS ] = { +"BPF syscall common attributes support" , probe_bpf_syscall_common_attrs, + }, };bool feat_supported (struct kern_feature_cache *cache,enum kern_feature_id feat_id)
tools/lib/bpf/libbpf_internal.h +3 -0
@@ -398,6 +398,8 @@ enum kern_feature_id {FEAT_UPROBE_SYSCALL ,/* Kernel supports BTF layout information */ FEAT_BTF_LAYOUT , +/* Kernel supports BPF syscall common attributes */ +FEAT_BPF_SYSCALL_COMMON_ATTRS , __FEAT_CNT, };
@@ -768,4 +770,5 @@ int probe_fd(int fd);#define SHA256_DWORD_SIZE SHA256_DIGEST_LENGTH / sizeof(__u64)void libbpf_sha256 (const void *data,size_t len,__u8 out[SHA256_DIGEST_LENGTH ]); +int probe_sys_bpf_ext (void );#endif /* __LIBBPF_LIBBPF_INTERNAL_H */
Signature · 1 line
2.54.0
PATCH v13 3/8 bpf: Refactor reporting log_true_size for prog_load
In reply to Leon HwangThe 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_log_attr_init(): initialize 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.
--- include/linux/bpf.h | 4 +++- include/linux/bpf_verifier.h | 12 ++++++++++++ kernel/bpf/log.c | 29 +++++++++++++++++++++++++++++ kernel/bpf/syscall.c | 12 +++++++++--- kernel/bpf/verifier.c | 17 ++++------------- 5 files changed, 57 insertions(+), 17 deletions(-)
include/linux/bpf.h +3 -1
@@ -2919,7 +2919,9 @@ int bpf_check_uarg_tail_zero(bpfptr_t uaddr, size_t expected_size, size_t actual_size);/* verify correctness of eBPF program */ -int bpf_check (struct bpf_prog **fp,union bpf_attr *attr,bpfptr_t uattr,u32 uattr_size); +struct bpf_log_attr ; +int bpf_check (struct bpf_prog **fp,union bpf_attr *attr,bpfptr_t uattr, +struct bpf_log_attr *attr_log);#ifndef CONFIG_BPF_JIT_ALWAYS_ON void bpf_patch_call_args (struct bpf_insn *insn,u32 stack_depth);
include/linux/bpf_verifier.h +12 -0
@@ -755,6 +755,18 @@ static inline bool bpf_verifier_log_needed(const struct bpf_verifier_log *log)return log && log->level; } +struct bpf_log_attr { +char __user *ubuf; +u32 size; +u32 level; +u32 offsetof_true_size; +bpfptr_t uattr; +}; + +int bpf_log_attr_init (struct bpf_log_attr *log,u64 log_buf,u32 log_size,u32 log_level, +u32 offsetof_log_true_size,bpfptr_t uattr); +int bpf_log_attr_finalize (struct bpf_log_attr *attr,struct bpf_verifier_log *log); +#define BPF_MAX_SUBPROGS 256struct bpf_subprog_arg_info {
kernel/bpf/log.c +29 -0
@@ -825,3 +825,32 @@ void print_insn_state(struct bpf_verifier_env *env, const struct bpf_verifier_st }print_verifier_state (env, vstate, frameno, false); } + +int bpf_log_attr_init (struct bpf_log_attr *log,u64 log_buf,u32 log_size,u32 log_level, +u32 offsetof_log_true_size,bpfptr_t uattr) +{ +char __user *ubuf =u64_to_user_ptr (log_buf); + +memset (log,0 ,sizeof (*log)); + log->ubuf = ubuf; + log->size = log_size; + log->level = log_level; + log->offsetof_true_size = offsetof_log_true_size; + log->uattr = uattr; +return 0 ; +} + +int bpf_log_attr_finalize (struct bpf_log_attr *attr,struct bpf_verifier_log *log) +{ +u32 log_true_size; +int err; + + err =bpf_vlog_finalize (log, &log_true_size); + +if (attr->offsetof_true_size && +copy_to_bpfptr_offset (attr->uattr, attr->offsetof_true_size, &log_true_size, +sizeof (log_true_size))) +return -EFAULT ; + +return err; +}
kernel/bpf/syscall.c +9 -3
@@ -2861,7 +2861,7 @@ static int bpf_prog_mark_insn_arrays_ready(struct bpf_prog *prog)/* last field in 'union bpf_attr' used by this command */ #define BPF_PROG_LOAD_LAST_FIELD keyring_id -static int bpf_prog_load (union bpf_attr *attr,bpfptr_t uattr,u32 uattr_size) +static int bpf_prog_load (union bpf_attr *attr,bpfptr_t uattr,struct bpf_log_attr *attr_log) {enum bpf_prog_type type = attr->prog_type;struct bpf_prog *prog, *dst_prog =NULL ;
@@ -3079,7 +3079,7 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size) goto free_prog_sec;/* run eBPF verifier */ - err =bpf_check (&prog, attr, uattr, uattr_size); + err =bpf_check (&prog, attr, uattr, attr_log);if (err <0 ) goto free_used_maps;
@@ -6215,6 +6215,8 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size, bpfptr_t uattr_common, unsigned int size_common) { struct bpf_common_attr attr_common; +u32 offsetof_log_true_size =0 ; +struct bpf_log_attr attr_log;union bpf_attr attr;int err;
@@ -6266,7 +6268,11 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size, err =map_freeze (&attr);break ;case BPF_PROG_LOAD : - err =bpf_prog_load (&attr, uattr, size); + if (size >= offsetofend(union bpf_attr, log_true_size)) + offsetof_log_true_size = offsetof(union bpf_attr , log_true_size); + err =bpf_log_attr_init (&attr_log, attr.log_buf, attr.log_size, attr.log_level, + offsetof_log_true_size, uattr); + err = err ?:bpf_prog_load (&attr, uattr, &attr_log);break ;case BPF_OBJ_PIN : err =bpf_obj_pin (&attr);
kernel/bpf/verifier.c +4 -13
@@ -19294,12 +19294,12 @@ int bpf_fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,return 0 ; } -int bpf_check (struct bpf_prog **prog,union bpf_attr *attr,bpfptr_t uattr,__u32 uattr_size) +int bpf_check (struct bpf_prog **prog,union bpf_attr *attr,bpfptr_t uattr, +struct bpf_log_attr *attr_log) {u64 start_time =ktime_get_ns ();struct bpf_verifier_env *env;int i, len, ret = -EINVAL , err; -u32 log_true_size;bool is_priv;BTF_TYPE_EMIT (enum bpf_features );
@@ -19346,9 +19346,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3/* user could have requested verbose verifier output * and supplied buffer to store the verification trace */ - ret =bpf_vlog_init (&env->log, attr->log_level, - (char __user *) (unsigned long ) attr->log_buf, - attr->log_size); + ret =bpf_vlog_init (&env->log, attr_log->level, attr_log->ubuf, attr_log->size);if (ret) goto err_unlock;
@@ -19510,17 +19508,10 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3 env->prog->aux->verified_insns = env->insn_processed;/* preserve original error even if log finalization is successful */ - err =bpf_vlog_finalize (&env->log, &log_true_size); + err =bpf_log_attr_finalize (attr_log, &env->log);if (err) ret = err; - if (uattr_size >= offsetofend(union bpf_attr, log_true_size) && -copy_to_bpfptr_offset (uattr, offsetof(union bpf_attr , log_true_size), - &log_true_size,sizeof (log_true_size))) { - ret = -EFAULT ; - goto err_release_maps; - } -if (ret) goto err_release_maps;
Signature · 1 line
2.54.0
PATCH v13 4/8 bpf: Add syscall common attributes support for prog_load
In reply to Leon HwangBPF_PROG_LOAD can now take log parameters from both union bpf_attr and struct bpf_common_attr. The merge rules are:
- if both sides provide a complete log tuple (buf/size/level) and they match, use it;
- if only one side provides log parameters, use that one;
- if both sides provide complete tuples but they differ, return -EINVAL.
--- include/linux/bpf_verifier.h | 3 ++- kernel/bpf/log.c | 34 +++++++++++++++++++++++++++------- kernel/bpf/syscall.c | 3 ++- 3 files changed, 31 insertions(+), 9 deletions(-)
include/linux/bpf_verifier.h +2 -1
@@ -764,7 +764,8 @@ struct bpf_log_attr { };int bpf_log_attr_init (struct bpf_log_attr *log,u64 log_buf,u32 log_size,u32 log_level, -u32 offsetof_log_true_size,bpfptr_t uattr); +u32 offsetof_log_true_size,bpfptr_t uattr,struct bpf_common_attr *common, +bpfptr_t uattr_common,u32 size_common);int bpf_log_attr_finalize (struct bpf_log_attr *attr,struct bpf_verifier_log *log);#define BPF_MAX_SUBPROGS 256
kernel/bpf/log.c +27 -7
@@ -13,17 +13,17 @@ #define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args) -static bool bpf_verifier_log_attr_valid (const struct bpf_verifier_log *log) +static bool bpf_verifier_log_attr_valid (u32 log_level,char __user *log_buf,u32 log_size) {/* ubuf and len_total should both be specified (or not) together */ -if (!!log->ubuf != !!log->len_total) +if (!!log_buf != !!log_size)return false;/* log buf without log_level is meaningless */ -if (log->ubuf && log->level ==0 ) +if (log_buf && log_level ==0 )return false; -if (log->level & ~BPF_LOG_MASK ) +if (log_level & ~BPF_LOG_MASK )return false; -if (log->len_total >UINT_MAX >>2 ) +if (log_size >UINT_MAX >>2 )return false;return true; }
@@ -36,7 +36,7 @@ int bpf_vlog_init(struct bpf_verifier_log *log, u32 log_level, log->len_total = log_size;/* log attributes have to be sane */ -if (!bpf_verifier_log_attr_valid (log)) +if (!bpf_verifier_log_attr_valid (log_level, log_buf, log_size))return -EINVAL ;return 0 ;
@@ -827,16 +827,36 @@ void print_insn_state(struct bpf_verifier_env *env, const struct bpf_verifier_st }int bpf_log_attr_init (struct bpf_log_attr *log,u64 log_buf,u32 log_size,u32 log_level, -u32 offsetof_log_true_size,bpfptr_t uattr) +u32 offsetof_log_true_size,bpfptr_t uattr,struct bpf_common_attr *common, +bpfptr_t uattr_common,u32 size_common) { +char __user *ubuf_common =u64_to_user_ptr (common->log_buf);char __user *ubuf =u64_to_user_ptr (log_buf); +if (!bpf_verifier_log_attr_valid (common->log_level, ubuf_common, common->log_size) || + !bpf_verifier_log_attr_valid (log_level, ubuf, log_size)) +return -EINVAL ; + +if (ubuf && ubuf_common && (ubuf != ubuf_common || log_size != common->log_size || + log_level != common->log_level)) +return -EINVAL ; +memset (log,0 ,sizeof (*log)); log->ubuf = ubuf; log->size = log_size; log->level = log_level; log->offsetof_true_size = offsetof_log_true_size; log->uattr = uattr; + +if (!ubuf && ubuf_common) { + log->ubuf = ubuf_common; + log->size = common->log_size; + log->level = common->log_level; + log->uattr = uattr_common; + log->offsetof_true_size =0 ; + if (size_common >= offsetofend(struct bpf_common_attr, log_true_size)) + log->offsetof_true_size = offsetof(struct bpf_common_attr , log_true_size); + }return 0 ; }
kernel/bpf/syscall.c +2 -1
@@ -6271,7 +6271,8 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size, if (size >= offsetofend(union bpf_attr, log_true_size)) offsetof_log_true_size = offsetof(union bpf_attr , log_true_size); err =bpf_log_attr_init (&attr_log, attr.log_buf, attr.log_size, attr.log_level, - offsetof_log_true_size, uattr); + offsetof_log_true_size, uattr, &attr_common, uattr_common, + size_common); err = err ?:bpf_prog_load (&attr, uattr, &attr_log);break ;case BPF_OBJ_PIN :
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH bpf-next v13 4/8] bpf: Add syscall common attributes support for prog_load
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] The log parameters merge logic incorrectly equates the presence of log parameters with non-NULL…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The log parameters merge logic incorrectly equates the presence of log parameters with non-NULL log buffers, silently ignoring valid log_level configurations and bypassing conflict checks.
- [Medium] When both specific and common attributes provide identical log parameters, log_true_size writeback may be lost if the specific attr is undersized.
--
commit b3df7b18c0d52179a3ed4e0c0f6906e0cee721ff
Author: Leon Hwang <[email protected]>
bpf: Add syscall common attributes support for prog_load
BPF_PROG_LOAD can now take log parameters from both union bpf_attr and struct bpf_common_attr. The merge rules are: match, use one, or return -EINVAL.
Quoted 4 lines
kernel/bpf/log.c +0
-0
[ ... ]
Quoted 8 lines
kernel/bpf/log.c +2
-1@@ -827,16 +827,36 @@ void print_insn_state(struct bpf_verifier_env *env, const struct bpf_verifier_st }int bpf_log_attr_init (struct bpf_log_attr *log,u64 log_buf,u32 log_size,u32 log_level, -u32 offsetof_log_true_size,bpfptr_t uattr) +u32 offsetof_log_true_size,bpfptr_t uattr,struct bpf_common_attr *common, +bpfptr_t uattr_common,u32 size_common) {
kernel/bpf/log.c void print_insn_state(struct bpf_verifier_env *env, const struct bpf_verifier_st · line 842
Quoted 10 lines
kernel/bpf/log.c +8
-0+char __user *ubuf_common =u64_to_user_ptr (common->log_buf);char __user *ubuf =u64_to_user_ptr (log_buf); +if (!bpf_verifier_log_attr_valid (common->log_level, ubuf_common, common->log_size) || + !bpf_verifier_log_attr_valid (log_level, ubuf, log_size)) +return -EINVAL ; + +if (ubuf && ubuf_common && (ubuf != ubuf_common || log_size != common->log_size || + log_level != common->log_level)) +return -EINVAL ;
If a user specifies log_level != 0 but leaves log_buf == NULL to probe for log_true_size, will these checks bypass conflicts?
If one side provides a complete tuple and the other provides only log_level, or if both provide mismatched log_levels without buffers, it appears the conflict checks are skipped and no -EINVAL is returned.
kernel/bpf/log.c void print_insn_state(struct bpf_verifier_env *env, const struct bpf_verifier_st · line 851
Quoted 9 lines
kernel/bpf/log.c +3
-0+memset (log,0 ,sizeof (*log)); log->ubuf = ubuf; log->size = log_size; log->level = log_level; log->offsetof_true_size = offsetof_log_true_size; log->uattr = uattr; + +if (!ubuf && ubuf_common) {
Does this condition silently ignore valid common->log_level configurations?
If the user provides only common->log_level via bpf_common_attr for a log size dry-run, ubuf_common evaluates to NULL. The condition here would evaluate to false, and common->log_level would be ignored instead of applied.
Also, if both specific and common attributes provide identical log parameters, the code appears to unconditionally use uattr for the log_true_size writeback since ubuf is not NULL.
If the original uattr uses an older layout without space for log_true_size, log->offsetof_true_size will be set to 0. Does this mean log_true_size writeback will be lost even if the user provided a modern attr_common struct with sufficient space to receive it?
Quoted 10 lines
kernel/bpf/log.c +8
-0+ log->ubuf = ubuf_common; + log->size = common->log_size; + log->level = common->log_level; + log->uattr = uattr_common; + log->offsetof_true_size =0 ; + if (size_common >= offsetofend(struct bpf_common_attr, log_true_size)) + log->offsetof_true_size = offsetof(struct bpf_common_attr , log_true_size); + }return 0 ; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Re: [PATCH bpf-next v13 4/8] bpf: Add syscall common attributes support for prog_load
On 13/5/26 06:18, [email protected] wrote:
[...]
Quoted 19 lines
Quoted 5 lines
int bpf_log_attr_init(struct
bpf_log_attr*log, u64log_buf, u32log_size, u32log_level,kernel/bpf/log.c +2
-1- u32 offsetof_log_true_size, bpfptr_t uattr) + u32 offsetof_log_true_size, bpfptr_t uattr, struct bpf_common_attr *common, + bpfptr_t uattr_common, u32 size_common) {kernel/bpf/log.c void print_insn_state(struct bpf_verifier_env *env, const struct bpf_verifier_st · line 842
Quoted 10 lines
kernel/bpf/log.c +8
-0+char __user *ubuf_common =u64_to_user_ptr (common->log_buf);char __user *ubuf =u64_to_user_ptr (log_buf); +if (!bpf_verifier_log_attr_valid (common->log_level, ubuf_common, common->log_size) || + !bpf_verifier_log_attr_valid (log_level, ubuf, log_size)) +return -EINVAL ; + +if (ubuf && ubuf_common && (ubuf != ubuf_common || log_size != common->log_size || + log_level != common->log_level)) +return -EINVAL ;If a user specifies
log_level!= 0 but leaveslog_buf== NULL to probe forlog_true_size, will these checks bypass conflicts?
Quoted 4 lines
If one side provides a complete tuple and the other provides only
log_level, or if both provide mismatchedlog_levelswithout buffers, it appears the conflict checks are skipped and no-EINVALis returned.
Correct.
However, if users want to probe for log_true_size, they should not provide common attrs. It is compatible for bpf libraries to probe for log_true_size using union bpf_attr without providing common attrs.
Quoted 12 lines
kernel/bpf/log.c void print_insn_state(struct bpf_verifier_env *env, const struct bpf_verifier_st · line 851
Quoted 9 lines
kernel/bpf/log.c +3
-0+memset (log,0 ,sizeof (*log)); log->ubuf = ubuf; log->size = log_size; log->level = log_level; log->offsetof_true_size = offsetof_log_true_size; log->uattr = uattr; + +if (!ubuf && ubuf_common) {Does this condition silently ignore valid
common->log_levelconfigurations?
Quoted 5 lines
If the user provides only
common->log_levelviabpf_common_attrfor a log size dry-run,ubuf_commonevaluates to NULL. The condition here would evaluate to false, andcommon->log_levelwould be ignored instead of applied.
The user should probe for log size using union bpf_attr instead of common attrs.
Quoted 9 lines
Also, if both specific and common attributes provide identical log parameters, the code appears to unconditionally use uattr for the
log_true_sizewriteback since ubuf is not NULL.If the original uattr uses an older layout without space for
log_true_size,log->offsetof_true_sizewill be set to 0. Does this meanlog_true_sizewriteback will be lost even if the user provided a modernattr_commonstruct with sufficient space to receive it?
This was intentional.
If the user uses union bpf_attr to receive log, he should provide the space for log_true_size.
IOW, if the user uses common attrs to receive log, he should provide the space in common attrs for log_true_size.
Thanks,
Leon
Quoted 11 lines
Quoted 10 lines
kernel/bpf/log.c +8
-0+ log->ubuf = ubuf_common; + log->size = common->log_size; + log->level = common->log_level; + log->uattr = uattr_common; + log->offsetof_true_size =0 ; + if (size_common >= offsetofend(struct bpf_common_attr, log_true_size)) + log->offsetof_true_size = offsetof(struct bpf_common_attr , log_true_size); + }return 0 ; }
PATCH v13 5/8 bpf: Add syscall common attributes support for btf_load
In reply to Leon HwangBPF_BTF_LOAD can now take log parameters from both union bpf_attr and struct bpf_common_attr, with the same merge rules as BPF_PROG_LOAD:
- if both sides provide a complete log tuple (buf/size/level) and they match, use it;
- if only one side provides log parameters, use that one;
- if both sides provide complete tuples but they differ, return -EINVAL.
--- include/linux/btf.h | 3 ++- kernel/bpf/btf.c | 30 +++++++----------------------- kernel/bpf/syscall.c | 11 ++++++++--- 3 files changed, 17 insertions(+), 27 deletions(-)
include/linux/btf.h +2 -1
@@ -145,7 +145,8 @@ const char *btf_get_name(const struct btf *btf);void btf_get (struct btf *btf);void btf_put (struct btf *btf);const struct btf_header *btf_header (const struct btf *btf); -int btf_new_fd (const union bpf_attr *attr,bpfptr_t uattr,u32 uattr_sz); +struct bpf_log_attr ; +int btf_new_fd (const union bpf_attr *attr,bpfptr_t uattr,struct bpf_log_attr *attr_log);struct btf *btf_get_by_fd (int fd); int btf_get_info_by_fd(const struct btf *btf, const union bpf_attr *attr,
kernel/bpf/btf.c +7 -23
@@ -5907,25 +5907,10 @@ static int btf_check_type_tags(struct btf_verifier_env *env,return 0 ; } -static int finalize_log (struct bpf_verifier_log *log,bpfptr_t uattr,u32 uattr_size) -{ -u32 log_true_size; -int err; - - err =bpf_vlog_finalize (log, &log_true_size); - - if (uattr_size >= offsetofend(union bpf_attr, btf_log_true_size) && -copy_to_bpfptr_offset (uattr, offsetof(union bpf_attr , btf_log_true_size), - &log_true_size,sizeof (log_true_size))) - err = -EFAULT ; - -return err; -} - -static struct btf *btf_parse (const union bpf_attr *attr,bpfptr_t uattr,u32 uattr_size) +static struct btf *btf_parse (const union bpf_attr *attr,bpfptr_t uattr, +struct bpf_log_attr *attr_log) {bpfptr_t btf_data =make_bpfptr (attr->btf, uattr.is_kernel); -char __user *log_ubuf =u64_to_user_ptr (attr->btf_log_buf);struct btf_struct_metas *struct_meta_tab;struct btf_verifier_env *env =NULL ;struct btf *btf =NULL ;
@@ -5942,8 +5927,7 @@ static struct btf *btf_parse(const union bpf_attr *attr, bpfptr_t uattr, u32 uat/* user could have requested verbose verifier output * and supplied buffer to store the verification trace */ - err =bpf_vlog_init (&env->log, attr->btf_log_level, - log_ubuf, attr->btf_log_size); + err =bpf_vlog_init (&env->log, attr_log->level, attr_log->ubuf, attr_log->size);if (err) goto errout_free;
@@ -6008,7 +5992,7 @@ static struct btf *btf_parse(const union bpf_attr *attr, bpfptr_t uattr, u32 uat } } - err =finalize_log (&env->log, uattr, uattr_size); + err =bpf_log_attr_finalize (attr_log, &env->log);if (err) goto errout_free;
@@ -6020,7 +6004,7 @@ static struct btf *btf_parse(const union bpf_attr *attr, bpfptr_t uattr, u32 uatbtf_free_struct_meta_tab (btf); errout:/* overwrite err with -ENOSPC or -EFAULT */ - ret =finalize_log (&env->log, uattr, uattr_size); + ret =bpf_log_attr_finalize (attr_log, &env->log);if (ret) err = ret; errout_free:
@@ -8189,12 +8173,12 @@ static int __btf_new_fd(struct btf *btf)return anon_inode_getfd ("btf" , &btf_fops, btf,O_RDONLY |O_CLOEXEC ); } -int btf_new_fd (const union bpf_attr *attr,bpfptr_t uattr,u32 uattr_size) +int btf_new_fd (const union bpf_attr *attr,bpfptr_t uattr,struct bpf_log_attr *attr_log) {struct btf *btf;int ret; - btf =btf_parse (attr, uattr, uattr_size); + btf =btf_parse (attr, uattr, attr_log);if (IS_ERR (btf))return PTR_ERR (btf);
kernel/bpf/syscall.c +8 -3
@@ -5474,7 +5474,7 @@ static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,#define BPF_BTF_LOAD_LAST_FIELD btf_token_fd -static int bpf_btf_load (const union bpf_attr *attr,bpfptr_t uattr,__u32 uattr_size) +static int bpf_btf_load (const union bpf_attr *attr,bpfptr_t uattr,struct bpf_log_attr *attr_log) {struct bpf_token *token =NULL ;
@@ -5501,7 +5501,7 @@ static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr, __u32 uattr_bpf_token_put (token); -return btf_new_fd (attr, uattr, uattr_size); +return btf_new_fd (attr, uattr, attr_log); }#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD fd_by_id_token_fd
@@ -6318,7 +6318,12 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size, err =bpf_raw_tracepoint_open (&attr);break ;case BPF_BTF_LOAD : - err =bpf_btf_load (&attr, uattr, size); + if (size >= offsetofend(union bpf_attr, btf_log_true_size)) + offsetof_log_true_size = offsetof(union bpf_attr , btf_log_true_size); + err =bpf_log_attr_init (&attr_log, attr.btf_log_buf, attr.btf_log_size, + attr.btf_log_level, offsetof_log_true_size, uattr, + &attr_common, uattr_common, size_common); + err = err ?:bpf_btf_load (&attr, uattr, &attr_log);break ;case BPF_BTF_GET_FD_BY_ID : err =bpf_btf_get_fd_by_id (&attr);
Signature · 1 line
2.54.0
PATCH v13 6/8 bpf: Add syscall common attributes support for map_create
In reply to Leon HwangMany 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.
--- include/linux/bpf_verifier.h | 3 ++ kernel/bpf/log.c | 29 ++++++++++++++++ kernel/bpf/syscall.c | 66 ++++++++++++++++++++++++++++++------ 3 files changed, 88 insertions(+), 10 deletions(-)
include/linux/bpf_verifier.h +3 -0
@@ -766,6 +766,9 @@ struct bpf_log_attr {int bpf_log_attr_init (struct bpf_log_attr *log,u64 log_buf,u32 log_size,u32 log_level,u32 offsetof_log_true_size,bpfptr_t uattr,struct bpf_common_attr *common,bpfptr_t uattr_common,u32 size_common); +struct bpf_verifier_log *bpf_log_attr_create_vlog (struct bpf_log_attr *attr_log, +struct bpf_common_attr *common,bpfptr_t uattr, +u32 size);int bpf_log_attr_finalize (struct bpf_log_attr *attr,struct bpf_verifier_log *log);#define BPF_MAX_SUBPROGS 256
kernel/bpf/log.c +29 -0
@@ -860,6 +860,35 @@ int bpf_log_attr_init(struct bpf_log_attr *log, u64 log_buf, u32 log_size, u32 lreturn 0 ; } +struct bpf_verifier_log *bpf_log_attr_create_vlog (struct bpf_log_attr *attr_log, +struct bpf_common_attr *common,bpfptr_t uattr, +u32 size) +{ +struct bpf_verifier_log *log; +int err; + +memset (attr_log,0 ,sizeof (*attr_log)); + attr_log->uattr = uattr; + if (size >= offsetofend(struct bpf_common_attr, log_true_size)) + attr_log->offsetof_true_size = offsetof(struct bpf_common_attr , log_true_size); + +if (!size) +return NULL ; + + log =kzalloc_obj (*log,GFP_KERNEL ); +if (!log) +return ERR_PTR (-ENOMEM ); + + err =bpf_vlog_init (log, common->log_level,u64_to_user_ptr (common->log_buf), + common->log_size); +if (err) { +kfree (log); +return ERR_PTR (err); + } + +return log; +} +int bpf_log_attr_finalize (struct bpf_log_attr *attr,struct bpf_verifier_log *log) {u32 log_true_size;
kernel/bpf/syscall.c +56 -10
148 lines not on this page · raw message
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH bpf-next v13 6/8] bpf: Add syscall common attributes support for map_create
When bpf_log_attr_finalize() returns an error (say -ENOSPC for log buffer truncation or -EFAULT for copy_to_user failure) after __map_create() already returned a negative error (for example -EINVAL…
Quoted 4 lines
include/linux/bpf_verifier.h +0
-0
[ ... ]
Quoted 4 lines
kernel/bpf/log.c +0
-0
[ ... ]
Quoted 4 lines
kernel/bpf/syscall.c +0
-0
[ ... ]
Quoted 12 lines
kernel/bpf/syscall.c +8
-0@@ -1611,6 +1632,31 @@ static int map_create(union bpf_attr *attr, bpfptr_t uattr)return err; } +static int map_create (union bpf_attr *attr,bpfptr_t uattr,struct bpf_common_attr *attr_common, +bpfptr_t uattr_common,u32 size_common) +{ +struct bpf_verifier_log *log; +struct bpf_log_attr attr_log; +int err, ret; + + log =bpf_log_attr_create_vlog (&attr_log, attr_common, uattr_common, size_common);
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bpfptr_t uattr) · line 1652
Quoted 10 lines
kernel/bpf/syscall.c +10
-0+if (IS_ERR (log)) +return PTR_ERR (log); + + err =__map_create (attr, uattr, log); + + ret =bpf_log_attr_finalize (&attr_log, log); +if (ret) { +if (err >=0 ) +close_fd (err); + err = ret;
When bpf_log_attr_finalize() returns an error (say -ENOSPC for log buffer truncation or -EFAULT for copy_to_user failure) after __map_create() already returned a negative error (for example -EINVAL from CHECK_ATTR or -EPERM from security checks), does it make sense to overwrite the original map creation error with the log finalization error?
The result is that userspace receives -ENOSPC or -EFAULT instead of the actual reason the map creation failed. A user with an undersized log buffer might retry with a larger buffer expecting success, when the real issue is that the map creation itself was rejected for a different reason.
Looking at bpf_check() in kernel/bpf/verifier.c (around the verifier log finalization), it uses similar logic with 'err = ret;' reassignment. However, that doesn't appear to have the preserve-original-error comment that would clarify the intended behavior for this scenario.
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bpfptr_t uattr) · line 1655
Quoted 3 lines
kernel/bpf/syscall.c +3
-0+ } + +free:
Does anything use this free: label? The only exit path before it is the early return for IS_ERR(log), which correctly returns directly since log is an error pointer and doesn't need cleanup. There doesn't seem to be any 'goto free;' statement in the function.
With -Wunused-label enabled by default in the kernel build (part of -Wall), this will generate a compiler warning. When building with -Werror (common in CI configurations and allmodconfig builds), this would break the build.
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bpfptr_t uattr) · line 1662
Quoted 7 lines
kernel/bpf/syscall.c +4
-0+kfree (log); +return err; +} +void bpf_map_inc (struct bpf_map *map) {atomic64_inc (&map->refcnt);
[ ... ]
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/25680759838
Re: [PATCH bpf-next v13 6/8] bpf: Add syscall common attributes support for map_create
Quoted 55 lines · bot+bpf-ci
Quoted 4 lines
include/linux/bpf_verifier.h +0
-0[ ... ]
Quoted 4 lines
kernel/bpf/log.c +0
-0[ ... ]
Quoted 4 lines
kernel/bpf/syscall.c +0
-0[ ... ]
Quoted 12 lines
kernel/bpf/syscall.c +8
-0@@ -1611,6 +1632,31 @@ static int map_create(union bpf_attr *attr, bpfptr_t uattr)return err; } +static int map_create (union bpf_attr *attr,bpfptr_t uattr,struct bpf_common_attr *attr_common, +bpfptr_t uattr_common,u32 size_common) +{ +struct bpf_verifier_log *log; +struct bpf_log_attr attr_log; +int err, ret; + + log =bpf_log_attr_create_vlog (&attr_log, attr_common, uattr_common, size_common);kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bpfptr_t uattr) · line 1652
Quoted 10 lines
kernel/bpf/syscall.c +10
-0+if (IS_ERR (log)) +return PTR_ERR (log); + + err =__map_create (attr, uattr, log); + + ret =bpf_log_attr_finalize (&attr_log, log); +if (ret) { +if (err >=0 ) +close_fd (err); + err = ret;When
bpf_log_attr_finalize()returns an error (say-ENOSPCfor log buffer truncation or-EFAULTforcopy_to_userfailure) after__map_create()already returned a negative error (for example-EINVALfromCHECK_ATTRor-EPERMfrom security checks), does it make sense to overwrite the original map creation error with the log finalization error?The result is that userspace receives
-ENOSPCor-EFAULTinstead of the actual reason the map creation failed. A user with an undersized log buffer might retry with a larger buffer expecting success, when the real issue is that the map creation itself was rejected for a different reason.
Quoted 4 lines
Looking at
bpf_check()inkernel/bpf/verifier.c(around the verifier log finalization), it uses similar logic with 'err = ret;' reassignment. However, that doesn't appear to have the preserve-original-error comment that would clarify the intended behavior for this scenario.
I guess sinle line comment wouldn't hurt.
Quoted 10 lines
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bpfptr_t uattr) · line 1655
Quoted 4 lines
kernel/bpf/syscall.c +3
-0+ } + +free:Does anything use this free: label? The only exit path before it is the early return for
IS_ERR(log), which correctly returns directly since log is an error pointer and doesn't need cleanup. There doesn't seem to be any 'goto free;' statement in the function.
Quoted 3 lines
With -Wunused-label enabled by default in the kernel build (part of -Wall), this will generate a compiler warning. When building with -Werror (common in CI configurations and allmodconfig builds), this would break the build.
good point. pls fix.
Re: [PATCH bpf-next v13 6/8] bpf: Add syscall common attributes support for map_create
Quoted 1 line · Alexei Starovoitov
On Mon May 11, 2026 at 9:19 AM PDT, bot+bpf-ci wrote:
[...]
Quoted 8 lines
Quoted 5 lines
Looking at
bpf_check()inkernel/bpf/verifier.c(around the verifier log finalization), it uses similar logic with 'err = ret;' reassignment. However, that doesn't appear to have the preserve-original-error comment that would clarify the intended behavior for this scenario.I guess sinle line comment wouldn't hurt.
Will add a single line comment.
Quoted 10 lines
Quoted 10 lines
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bpfptr_t uattr) · line 1655
Quoted 4 lines
kernel/bpf/syscall.c +3
-0+ } + +free:Does anything use this free: label? The only exit path before it is the early return for
IS_ERR(log), which correctly returns directly since log is an error pointer and doesn't need cleanup. There doesn't seem to be any 'goto free;' statement in the function.
Quoted 5 lines
Quoted 3 lines
With -Wunused-label enabled by default in the kernel build (part of -Wall), this will generate a compiler warning. When building with -Werror (common in CI configurations and allmodconfig builds), this would break the build.
good point. pls fix.
Will fix.
Thanks,
Leon
[email protected]
AI reviewRe: [PATCH bpf-next v13 6/8] bpf: Add syscall common attributes support for map_create
In reply to Leon HwangThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] Calling `close_fd()` on a published file descriptor introduces a critical race condition (File…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] Calling close_fd() on a published file descriptor introduces a critical race condition (File Descriptor Hijacking). --
commit e059afefe67c83f634eb8a27db293cda839d9654
Author: Leon Hwang <[email protected]>
bpf: Add syscall common attributes support for map_create
This commit introduces common attributes support and verifier logging to the map creation process. It refactors the map creation logic to allow reporting map-creation failure reasons back to userspace.
Quoted 4 lines
kernel/bpf/syscall.c +0
-0
[ ... ]
Quoted 11 lines
kernel/bpf/syscall.c +7
-0@@ -1611,6 +1632,31 @@ static int map_create(union bpf_attr *attr, bpfptr_t uattr)return err; } +static int map_create (union bpf_attr *attr,bpfptr_t uattr,struct bpf_common_attr *attr_common, +bpfptr_t uattr_common,u32 size_common) +{ +struct bpf_verifier_log *log; +struct bpf_log_attr attr_log; +int err, ret; +
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bpfptr_t uattr) · line 1651
Quoted 10 lines
kernel/bpf/syscall.c +10
-0+ log =bpf_log_attr_create_vlog (&attr_log, attr_common, uattr_common, size_common); +if (IS_ERR (log)) +return PTR_ERR (log); + + err =__map_create (attr, uattr, log); + + ret =bpf_log_attr_finalize (&attr_log, log); +if (ret) { +if (err >=0 ) +close_fd (err);
Is it safe to call close_fd() here? Since __map_create() uses bpf_map_new_fd() to allocate and publish the file descriptor via fd_install(), the file descriptor is already visible to userspace.
Could this introduce a regression where a concurrent thread closes this file descriptor and opens a new, unrelated file that receives the exact same fd number? If that happens, it seems this close_fd() might inadvertently close the unrelated file.
Additionally, since the map is published globally via its ID before fd installation in bpf_map_alloc_id(), could another thread acquire a reference to the map before this error path? It looks like the map might remain alive and accessible even though the syscall returns a failure.
Quoted 7 lines
kernel/bpf/syscall.c +7
-0+ err = ret; + } + +free: +kfree (log); +return err; +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Re: [PATCH bpf-next v13 6/8] bpf: Add syscall common attributes support for map_create
On 13/5/26 07:36, [email protected] wrote:
[...]
Quoted 12 lines
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bpfptr_t uattr) · line 1651
Quoted 7 lines
kernel/bpf/syscall.c +7
-0+ + err =__map_create (attr, uattr, log); + + ret =bpf_log_attr_finalize (&attr_log, log); +if (ret) { +if (err >=0 ) +close_fd (err);Is it safe to call
close_fd()here? Since__map_create()usesbpf_map_new_fd()to allocate and publish the file descriptor viafd_install(), the file descriptor is already visible to userspace.
I think it is acceptable to close_fd() here, that the duration between fd_install() and close_fd() is short.
Quoted 5 lines
Could this introduce a regression where a concurrent thread closes this file descriptor and opens a new, unrelated file that receives the exact same fd number? If that happens, it seems this
close_fd()might inadvertently close the unrelated file.
This regression is valid.
However, since the new fd hasn't been returned to user space, user space thread seems unable to get the exact fd number to close it.
Quoted 5 lines
Additionally, since the map is published globally via its ID before fd installation in
bpf_map_alloc_id(), could another thread acquire a reference to the map before this error path? It looks like the map might remain alive and accessible even though the syscall returns a failure.
I think this is not an issue, as the map is valid for other threads. The map's lifecycle is maintained by its own refcnt. close_fd() will dec the refcnt instead of destroying the map.
Thanks,
Leon
Quoted 8 lines
Quoted 7 lines
kernel/bpf/syscall.c +7
-0+ err = ret; + } + +free: +kfree (log); +return err; +}
Re: [PATCH bpf-next v13 6/8] bpf: Add syscall common attributes support for map_create
Quoted 16 lines · Leon Hwang
On 13/5/26 07:36, [email protected] wrote:
[...]Quoted 12 lines
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bpfptr_t uattr) · line 1651
Quoted 7 lines
kernel/bpf/syscall.c +7
-0+ + err =__map_create (attr, uattr, log); + + ret =bpf_log_attr_finalize (&attr_log, log); +if (ret) { +if (err >=0 ) +close_fd (err);Is it safe to call
close_fd()here? Since__map_create()usesbpf_map_new_fd()to allocate and publish the file descriptor viafd_install(), the file descriptor is already visible to userspace.
Quoted 2 lines
I think it is acceptable to
close_fd()here, that the duration betweenfd_install()andclose_fd()is short.
bot is correct. Let's avoid these races.
Pls move bpf_log_attr_finalize() into map_create and do it before security_bpf_map_create.
Re: [PATCH bpf-next v13 6/8] bpf: Add syscall common attributes support for map_create
Quoted 17 lines · Alexei Starovoitov
Quoted 16 lines · Leon Hwang
On 13/5/26 07:36, [email protected] wrote:
[...]Quoted 12 lines
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bpfptr_t uattr) · line 1651
Quoted 7 lines
kernel/bpf/syscall.c +7
-0+ + err =__map_create (attr, uattr, log); + + ret =bpf_log_attr_finalize (&attr_log, log); +if (ret) { +if (err >=0 ) +close_fd (err);Is it safe to call
close_fd()here? Since__map_create()usesbpf_map_new_fd()to allocate and publish the file descriptor viafd_install(), the file descriptor is already visible to userspace.
Quoted 6 lines
Quoted 2 lines
I think it is acceptable to
close_fd()here, that the duration betweenfd_install()andclose_fd()is short.bot is correct. Let's avoid these races.
Pls movebpf_log_attr_finalize()intomap_createand do it beforesecurity_bpf_map_create.
Will do it.
Thanks,
Leon
PATCH v13 7/8 libbpf: Add syscall common attributes support for map_create
In reply to Leon HwangWith 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.
--- tools/lib/bpf/bpf.c | 16 +++++++++++++++- tools/lib/bpf/bpf.h | 17 ++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-)
tools/lib/bpf/bpf.c +15 -1
@@ -209,6 +209,9 @@ int bpf_map_create(enum bpf_map_type map_type, const struct bpf_map_create_opts *opts) { const size_t attr_sz = offsetofend(union bpf_attr, excl_prog_hash_size); +const size_t attr_common_sz =sizeof (struct bpf_common_attr ); +struct bpf_common_attr attr_common; +struct bpf_log_opts *log_opts;union bpf_attr attr;int fd;
@@ -242,7 +245,18 @@ int bpf_map_create(enum bpf_map_type map_type, attr.excl_prog_hash =ptr_to_u64 (OPTS_GET (opts, excl_prog_hash,NULL )); attr.excl_prog_hash_size =OPTS_GET (opts, excl_prog_hash_size,0 ); - fd =sys_bpf_fd (BPF_MAP_CREATE , &attr, attr_sz); + log_opts =OPTS_GET (opts, log_opts,NULL ); +if (log_opts &&feat_supported (NULL ,FEAT_BPF_SYSCALL_COMMON_ATTRS )) { +memset (&attr_common,0 , attr_common_sz); + attr_common.log_buf =ptr_to_u64 (OPTS_GET (log_opts, buf,NULL )); + attr_common.log_size =OPTS_GET (log_opts, size,0 ); + attr_common.log_level =OPTS_GET (log_opts, level,0 ); + fd =sys_bpf_ext_fd (BPF_MAP_CREATE , &attr, attr_sz, &attr_common, attr_common_sz); +OPTS_SET (log_opts, true_size, attr_common.log_true_size); + }else { + fd =sys_bpf_fd (BPF_MAP_CREATE , &attr, attr_sz); +OPTS_SET (log_opts, true_size,0 ); + }return libbpf_err_errno (fd); }
tools/lib/bpf/bpf.h +16 -1
@@ -37,6 +37,18 @@ extern "C" { LIBBPF_API int libbpf_set_memlock_rlim(size_t memlock_bytes); +struct bpf_log_opts { +size_t sz;/* size of this struct for forward/backward compatibility */ + +char *buf; +__u32 size; +__u32 level; +__u32 true_size;/* out parameter set by kernel */ + + size_t :0; +}; +#define bpf_log_opts__last_field true_size +struct bpf_map_create_opts {size_t sz;/* size of this struct for forward/backward compatibility */
@@ -57,9 +69,12 @@ struct bpf_map_create_opts {const void *excl_prog_hash;__u32 excl_prog_hash_size; + +struct bpf_log_opts *log_opts; + size_t :0; }; -#define bpf_map_create_opts__last_field excl_prog_hash_size +#define bpf_map_create_opts__last_field log_opts LIBBPF_API int bpf_map_create(enum bpf_map_type map_type, const char *map_name,
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH bpf-next v13 7/8] libbpf: Add syscall common attributes support for map_create
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Missing OPTS_VALID() validation for the nested log_opts structure breaks forward compatibility. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Missing OPTS_VALID() validation for the nested log_opts structure breaks forward compatibility. --
commit 25b6e95560edf0a2b76e5b2fc840a6200407a730
Author: Leon Hwang <[email protected]>
libbpf: Add syscall common attributes support for map_create
This commit introduces bpf_log_opts and extends bpf_map_create_opts to allow users to capture detailed log messages during map creation failures. It passes these common attributes via the extended sys_bpf_ext_fd() call.
Quoted 3 lines
tools/lib/bpf/bpf.c +0
-0
[ ... ]
tools/lib/bpf/bpf.c int bpf_map_create(enum bpf_map_type map_type, · line 249
Quoted 7 lines
tools/lib/bpf/bpf.c +2
-1@@ -242,7 +245,18 @@ int bpf_map_create(enum bpf_map_type map_type, attr.excl_prog_hash =ptr_to_u64 (OPTS_GET (opts, excl_prog_hash,NULL )); attr.excl_prog_hash_size =OPTS_GET (opts, excl_prog_hash_size,0 ); - fd =sys_bpf_fd (BPF_MAP_CREATE , &attr, attr_sz); + log_opts =OPTS_GET (opts, log_opts,NULL ); +if (log_opts &&feat_supported (NULL ,FEAT_BPF_SYSCALL_COMMON_ATTRS )) {
Should there be an OPTS_VALID() check for the nested log_opts structure before we start extracting its fields?
While the parent opts structure is correctly validated with OPTS_VALID() at the start of bpf_map_create(), log_opts is a newly introduced option structure with its own sz field for forward and backward compatibility.
If an application compiled against a future libbpf header passes a bpf_log_opts struct with new, non-zero fields, wouldn't this silently ignore those fields instead of returning -EINVAL?
Quoted 4 lines
tools/lib/bpf/bpf.c +4
-0+memset (&attr_common,0 , attr_common_sz); + attr_common.log_buf =ptr_to_u64 (OPTS_GET (log_opts, buf,NULL )); + attr_common.log_size =OPTS_GET (log_opts, size,0 ); + attr_common.log_level =OPTS_GET (log_opts, level,0 );
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Re: [PATCH bpf-next v13 7/8] libbpf: Add syscall common attributes support for map_create
On 13/5/26 07:56, [email protected] wrote:
[...]
Quoted 8 lines
tools/lib/bpf/bpf.c int bpf_map_create(enum bpf_map_type map_type, · line 249
Quoted 4 lines
tools/lib/bpf/bpf.c +2
-1- fd =sys_bpf_fd (BPF_MAP_CREATE , &attr, attr_sz); + log_opts =OPTS_GET (opts, log_opts,NULL ); +if (log_opts &&feat_supported (NULL ,FEAT_BPF_SYSCALL_COMMON_ATTRS )) {Should there be an
OPTS_VALID()check for the nestedlog_optsstructure before we start extracting its fields?
Quoted 8 lines
While the parent opts structure is correctly validated with
OPTS_VALID()at the start ofbpf_map_create(),log_optsis a newly introduced option structure with its own sz field for forward and backward compatibility.If an application compiled against a future libbpf header passes a
bpf_log_optsstruct with new, non-zero fields, wouldn't this silently ignore those fields instead of returning-EINVAL?
Good catch.
OPTS_VALID() is missed for log_opts here.
Will post a follow-up patch after the sashiko reviews for v14.
Thanks,
Leon
Quoted 5 lines
Quoted 4 lines
tools/lib/bpf/bpf.c +4
-0+memset (&attr_common,0 , attr_common_sz); + attr_common.log_buf =ptr_to_u64 (OPTS_GET (log_opts, buf,NULL )); + attr_common.log_size =OPTS_GET (log_opts, size,0 ); + attr_common.log_level =OPTS_GET (log_opts, level,0 );
PATCH v13 8/8 selftests/bpf: Add tests to verify map create failure log
In reply to Leon HwangAdd tests to verify that the kernel reports the expected error messages and correct log_true_size when map creation fails.
--- .../selftests/bpf/prog_tests/map_init.c | 166 ++++++++++++++++++ 1 file changed, 166 insertions(+)
tools/testing/selftests/bpf/prog_tests/map_init.c +166 -0
@@ -212,3 +212,169 @@ void test_map_init(void)if (test__start_subtest ("pcpu_lru_map_init" ))test_pcpu_lru_map_init (); } + +static void test_map_create (enum bpf_map_type map_type,const char *map_name, +struct bpf_map_create_opts *opts,const char *exp_msg) +{ +const int key_size =4 , value_size =4 , max_entries =1 ; +char log_buf[128 ]; +int fd; +LIBBPF_OPTS (bpf_log_opts, log_opts); + + log_buf[0 ] ='\0' ; + log_opts.buf = log_buf; + log_opts.size =sizeof (log_buf); + log_opts.level =1 ; + opts->log_opts = &log_opts; + fd =bpf_map_create (map_type, map_name, key_size, value_size, max_entries, opts); +if (!ASSERT_LT (fd,0 ,"bpf_map_create" )) { +close (fd); +return ; + } + +ASSERT_STREQ (log_buf, exp_msg,"log_buf" ); +ASSERT_EQ (log_opts.true_size,strlen (exp_msg) +1 ,"true_size" ); +} + +static void test_map_create_array (struct bpf_map_create_opts *opts,const char *exp_msg) +{ +test_map_create (BPF_MAP_TYPE_ARRAY ,"test_map_create" , opts, exp_msg); +} + +static void test_invalid_vmlinux_value_type_id_struct_ops (void ) +{ +const char *msg ="btf_vmlinux_value_type_id can only be used with struct_ops maps.\n" ; +LIBBPF_OPTS (bpf_map_create_opts, opts, + .btf_vmlinux_value_type_id = 1, + ); + +test_map_create_array (&opts, msg); +} + +static void test_invalid_vmlinux_value_type_id_kv_type_id (void ) +{ +const char *msg ="btf_vmlinux_value_type_id is mutually exclusive with btf_key_type_id and btf_value_type_id.\n" ; +LIBBPF_OPTS (bpf_map_create_opts, opts, + .btf_vmlinux_value_type_id = 1, + .btf_key_type_id = 1, + ); + +test_map_create (BPF_MAP_TYPE_STRUCT_OPS ,"test_map_create" , &opts, msg); +} + +static void test_invalid_value_type_id (void ) +{ +const char *msg ="Invalid btf_value_type_id.\n" ; +LIBBPF_OPTS (bpf_map_create_opts, opts, + .btf_key_type_id = 1, + ); + +test_map_create_array (&opts, msg); +} + +static void test_invalid_map_extra (void ) +{ +const char *msg ="Invalid map_extra.\n" ; +LIBBPF_OPTS (bpf_map_create_opts, opts, + .map_extra = 1, + ); + +test_map_create_array (&opts, msg); +} + +static void test_invalid_numa_node (void ) +{ +const char *msg ="Invalid numa_node.\n" ; +LIBBPF_OPTS (bpf_map_create_opts, opts, + .map_flags = BPF_F_NUMA_NODE, + .numa_node = 0xFF, + ); + +test_map_create_array (&opts, msg); +} + +static void test_invalid_map_type (void ) +{ +const char *msg ="Invalid map_type.\n" ; +LIBBPF_OPTS (bpf_map_create_opts, opts); + +test_map_create (__MAX_BPF_MAP_TYPE,"test_map_create" , &opts, msg); +} + +static void test_invalid_token_fd (void ) +{ +const char *msg ="Invalid map_token_fd.\n" ; +LIBBPF_OPTS (bpf_map_create_opts, opts, + .map_flags = BPF_F_TOKEN_FD, + .token_fd = 0xFF, + ); + +test_map_create_array (&opts, msg); +} + +static void test_invalid_map_name (void ) +{ +const char *msg ="Invalid map_name.\n" ; +LIBBPF_OPTS (bpf_map_create_opts, opts); + +test_map_create (BPF_MAP_TYPE_ARRAY ,"test-!@#" , &opts, msg); +} + +static void test_invalid_btf_fd (void ) +{ +const char *msg ="Invalid btf_fd.\n" ; +LIBBPF_OPTS (bpf_map_create_opts, opts, + .btf_fd = -1, + .btf_key_type_id = 1, + .btf_value_type_id = 1, + ); + +test_map_create_array (&opts, msg); +} + +static void test_excl_prog_hash_size_1 (void ) +{ +const char *msg ="Invalid excl_prog_hash_size.\n" ; +const char *hash ="DEADCODE" ; +LIBBPF_OPTS (bpf_map_create_opts, opts, + .excl_prog_hash = hash, + ); + +test_map_create_array (&opts, msg); +} + +static void test_excl_prog_hash_size_2 (void ) +{ +const char *msg ="Invalid excl_prog_hash_size.\n" ; +LIBBPF_OPTS (bpf_map_create_opts, opts, + .excl_prog_hash_size = 1, + ); + +test_map_create_array (&opts, msg); +} + +void test_map_create_failure (void ) +{ +if (test__start_subtest ("invalid_vmlinux_value_type_id_struct_ops" )) +test_invalid_vmlinux_value_type_id_struct_ops (); +if (test__start_subtest ("invalid_vmlinux_value_type_id_kv_type_id" )) +test_invalid_vmlinux_value_type_id_kv_type_id (); +if (test__start_subtest ("invalid_value_type_id" )) +test_invalid_value_type_id (); +if (test__start_subtest ("invalid_map_extra" )) +test_invalid_map_extra (); +if (test__start_subtest ("invalid_numa_node" )) +test_invalid_numa_node (); +if (test__start_subtest ("invalid_map_type" )) +test_invalid_map_type (); +if (test__start_subtest ("invalid_token_fd" )) +test_invalid_token_fd (); +if (test__start_subtest ("invalid_map_name" )) +test_invalid_map_name (); +if (test__start_subtest ("invalid_btf_fd" )) +test_invalid_btf_fd (); +if (test__start_subtest ("invalid_excl_prog_hash_size_1" )) +test_excl_prog_hash_size_1 (); +if (test__start_subtest ("invalid_excl_prog_hash_size_2" )) +test_excl_prog_hash_size_2 (); +}
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH bpf-next v13 8/8] selftests/bpf: Add tests to verify map create failure log
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The tests `test_invalid_numa_node` and `test_invalid_token_fd` use the hardcoded value `0xFF`…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The tests test_invalid_numa_node and test_invalid_token_fd use the hardcoded value 0xFF (255) to trigger validation failures, which can cause spurious test failures on certain systems. --
commit 31093136dd05cf604e6a2e0d0a470807567ed4a7
Author: Leon Hwang <[email protected]>
selftests/bpf: Add tests to verify map create failure log
This commit adds tests to verify that the kernel reports the expected error messages and correct log_true_size when map creation fails.
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/map_init.c +0
-0
[ ... ]
tools/testing/selftests/bpf/prog_tests/map_init.c void test_map_init(void) · line 290
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/map_init.c +6
-0+static void test_invalid_numa_node (void ) +{ +const char *msg ="Invalid numa_node.\n" ; + LIBBPF_OPTS(bpf_map_create_opts, opts, + .map_flags = BPF_F_NUMA_NODE, + .numa_node = 0xFF,
Is it possible for this test to fail spuriously on large systems? If the test runs on a machine with 256 or more NUMA nodes where node 255 is online, the kernel validation will pass.
The map creation might then fail for another reason, skipping the expected log message and causing the test to fail.
Would a value like 0xFFFFFFFE (avoiding NUMA_NO_NODE) be more reliable to ensure it exceeds nr_node_ids?
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/map_init.c +4
-0+ ); + +test_map_create_array (&opts, msg); +}
[ ... ]
tools/testing/selftests/bpf/prog_tests/map_init.c void test_map_init(void) · line 309
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/map_init.c +6
-0+static void test_invalid_token_fd (void ) +{ +const char *msg ="Invalid map_token_fd.\n" ; + LIBBPF_OPTS(bpf_map_create_opts, opts, + .map_flags = BPF_F_TOKEN_FD, + .token_fd = 0xFF,
Could this also cause an unexpected test failure if file descriptor 255 happens to be an open BPF token in the test runner?
It looks like test_invalid_btf_fd uses -1 to guarantee an invalid file descriptor. Does it make sense to use -1 here as well?
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/map_init.c +4
-0+ ); + +test_map_create_array (&opts, msg); +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Re: [PATCH bpf-next v13 8/8] selftests/bpf: Add tests to verify map create failure log
On 13/5/26 08:33, [email protected] wrote:
[...]
Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/map_init.c void test_map_init(void) · line 290
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/map_init.c +6
-0+static void test_invalid_numa_node (void ) +{ +const char *msg ="Invalid numa_node.\n" ; + LIBBPF_OPTS(bpf_map_create_opts, opts, + .map_flags = BPF_F_NUMA_NODE, + .numa_node = 0xFF,Is it possible for this test to fail spuriously on large systems? If the test runs on a machine with 256 or more NUMA nodes where node 255 is online, the kernel validation will pass.
Quoted 6 lines
The map creation might then fail for another reason, skipping the expected log message and causing the test to fail.
Would a value like
0xFFFFFFFE(avoidingNUMA_NO_NODE) be more reliable to ensure it exceedsnr_node_ids?
Practically, NUMA nodes number wouldn't be >=255.
Instead of 0xFFFFFFFE, better to get possible numa node number from /sys/devices/system/node/possible, like libbpf_num_possible_cpus() to get possible cpu number.
Quoted 17 lines
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/map_init.c +4
-0+ ); + +test_map_create_array (&opts, msg); +}[ ... ]
tools/testing/selftests/bpf/prog_tests/map_init.c void test_map_init(void) · line 309
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/map_init.c +6
-0+static void test_invalid_token_fd (void ) +{ +const char *msg ="Invalid map_token_fd.\n" ; + LIBBPF_OPTS(bpf_map_create_opts, opts, + .map_flags = BPF_F_TOKEN_FD, + .token_fd = 0xFF,Could this also cause an unexpected test failure if file descriptor 255 happens to be an open BPF token in the test runner?
Quoted 3 lines
It looks like
test_invalid_btf_fduses -1 to guarantee an invalid file descriptor. Does it make sense to use -1 here as well?
Make sense.
Will post a follow-up patch to use -1 here.
Thanks,
Leon
Quoted 5 lines
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/map_init.c +4
-0+ ); + +test_map_create_array (&opts, msg); +}
Re: [PATCH bpf-next v13 8/8] selftests/bpf: Add tests to verify map create failure log
Quoted 21 lines · Leon Hwang
On 13/5/26 08:33, [email protected] wrote:
[...]Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/map_init.c void test_map_init(void) · line 290
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/map_init.c +6
-0+static void test_invalid_numa_node (void ) +{ +const char *msg ="Invalid numa_node.\n" ; + LIBBPF_OPTS(bpf_map_create_opts, opts, + .map_flags = BPF_F_NUMA_NODE, + .numa_node = 0xFF,Is it possible for this test to fail spuriously on large systems? If the test runs on a machine with 256 or more NUMA nodes where node 255 is online, the kernel validation will pass.
Quoted 6 lines
The map creation might then fail for another reason, skipping the expected log message and causing the test to fail.
Would a value like
0xFFFFFFFE(avoidingNUMA_NO_NODE) be more reliable to ensure it exceedsnr_node_ids?
Quoted 5 lines
Practically, NUMA nodes number wouldn't be >=255.
Instead of
0xFFFFFFFE, better to get possible numa node number from/sys/devices/system/node/possible, likelibbpf_num_possible_cpus()to get possible cpu number.
Ignore this bit. There won't be such numa nodes any time soon.
Re: [PATCH bpf-next v13 8/8] selftests/bpf: Add tests to verify map create failure log
Quoted 22 lines · Alexei Starovoitov
Quoted 21 lines · Leon Hwang
On 13/5/26 08:33, [email protected] wrote:
[...]Quoted 17 lines
tools/testing/selftests/bpf/prog_tests/map_init.c void test_map_init(void) · line 290
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/map_init.c +6
-0+static void test_invalid_numa_node (void ) +{ +const char *msg ="Invalid numa_node.\n" ; + LIBBPF_OPTS(bpf_map_create_opts, opts, + .map_flags = BPF_F_NUMA_NODE, + .numa_node = 0xFF,Is it possible for this test to fail spuriously on large systems? If the test runs on a machine with 256 or more NUMA nodes where node 255 is online, the kernel validation will pass.
The map creation might then fail for another reason, skipping the expected log message and causing the test to fail.
Would a value like
0xFFFFFFFE(avoidingNUMA_NO_NODE) be more reliable to ensure it exceedsnr_node_ids?
Quoted 7 lines
Quoted 5 lines
Practically, NUMA nodes number wouldn't be >=255.
Instead of
0xFFFFFFFE, better to get possible numa node number from/sys/devices/system/node/possible, likelibbpf_num_possible_cpus()to get possible cpu number.Ignore this bit. There won't be such numa nodes any time soon.
Get it.
Thanks,
Leon