bpf: Extend bpf syscall with common attributes support
Subsystems (6)
v2 cover bpf: Extend bpf syscall with common attributes support
This proposal builds upon the discussion in
"[PATCH bpf-next v4 0/4] bpf: Improve error reporting for freplace attachment failure"[1], and is also relevant to ongoing efforts such as tracing multi-link attach failures[2].
This patch set introduces support for *common attributes* in the 'bpf()' syscall, providing a unified mechanism for passing shared metadata across all BPF commands.
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.
With this extension, the 'bpf()' syscall will be able to return meaningful error messages (e.g., failures of creating map), improving debuggability and user experience.
Changes:
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.
Links:
[1] https://lore.kernel.org/bpf/[email protected]/
[2] https://lore.kernel.org/bpf/[email protected]/
Leon Hwang (6):
bpf: Extend bpf syscall with common attributes support
libbpf: Add support for extended bpf syscall
bpf: Add common attr support for prog_load and btf_load
bpf: Add common attr support for map_create
libbpf: Add common attr support for map_create selftests/bpf: Add cases to test map create failure log
include/linux/bpf.h | 3 +- include/linux/bpf_verifier.h | 2 +- include/linux/btf.h | 3 +- include/linux/syscalls.h | 3 +- include/uapi/linux/bpf.h | 7 + kernel/bpf/btf.c | 12 +- kernel/bpf/log.c | 23 +++- kernel/bpf/syscall.c | 115 ++++++++++++---- kernel/bpf/verifier.c | 8 +- tools/include/uapi/linux/bpf.h | 7 + tools/lib/bpf/bpf.c | 61 ++++++++- tools/lib/bpf/bpf.h | 6 +- tools/lib/bpf/features.c | 8 ++ tools/lib/bpf/libbpf.map | 2 + tools/lib/bpf/libbpf_internal.h | 2 + .../selftests/bpf/prog_tests/map_init.c | 124 ++++++++++++++++++ 16 files changed, 341 insertions(+), 45 deletions(-)
--
2.50.1
RFC PATCH v2 1/6 bpf: Extend bpf syscall with common attributes support
This patch extends 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
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.
--- include/linux/syscalls.h | 3 ++- include/uapi/linux/bpf.h | 7 +++++++ kernel/bpf/syscall.c | 19 +++++++++++++++---- tools/include/uapi/linux/bpf.h | 7 +++++++ 4 files changed, 31 insertions(+), 5 deletions(-)
include/linux/syscalls.h +2 -1
@@ -933,7 +933,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 +7 -0
@@ -1474,6 +1474,13 @@ struct bpf_stack_build_id { }; }; +struct bpf_common_attr { +__u64 log_buf; +__u32 log_size; +__u32 log_level; +}; + +#define BPF_COMMON_ATTRS (1 << 16)#define BPF_OBJ_NAME_LEN 16Uenum {
kernel/bpf/syscall.c +15 -4
@@ -5987,8 +5987,10 @@ static int prog_stream_read(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 common_attrs;union bpf_attr attr;int err;
@@ -6002,6 +6004,14 @@ 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 (&common_attrs,0 ,sizeof (common_attrs)); +if (cmd &BPF_COMMON_ATTRS ) { + cmd &= ~BPF_COMMON_ATTRS ; + size_common =min_t (u32, size_common,sizeof (common_attrs)); +if (uattr_common.user &©_from_bpfptr (&common_attrs, uattr_common, size_common)) +return -EFAULT ; + } + err =security_bpf (cmd, &attr, size, uattr.is_kernel);if (err <0 )return err;
@@ -6134,9 +6144,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,
@@ -6167,7 +6178,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 +7 -0
@@ -1474,6 +1474,13 @@ struct bpf_stack_build_id { }; }; +struct bpf_common_attr { +__u64 log_buf; +__u32 log_size; +__u32 log_level; +}; + +#define BPF_COMMON_ATTRS (1 << 16)#define BPF_OBJ_NAME_LEN 16Uenum {
Signature · 1 line
2.50.1
Quoted 45 lines · Leon Hwang
This patch extends 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 levelThese 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 <[email protected]>--- include/linux/syscalls.h | 3 ++- include/uapi/linux/bpf.h | 7 +++++++ kernel/bpf/syscall.c | 19 +++++++++++++++---- tools/include/uapi/linux/bpf.h | 7 +++++++ 4 files changed, 31 insertions(+), 5 deletions(-)include/linux/syscalls.h +2
-1@@ -933,7 +933,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 +0
-0
include/uapi/linux/bpf.h struct bpf_stack_build_id { · line 1483
Quoted 10 lines
include/uapi/linux/bpf.h +7
-0@@ -1474,6 +1474,13 @@ struct bpf_stack_build_id { }; }; +struct bpf_common_attr { +__u64 log_buf; +__u32 log_size; +__u32 log_level; +}; + +#define BPF_COMMON_ATTRS (1 << 16)
add this into enum bpf_cmd after __MAX_BPF_CMD (with a small comment about the purpose of this)? That will keep everything cmd-related in one place
Quoted 20 lines
include/uapi/linux/bpf.h +0
-0#define BPF_OBJ_NAME_LEN 16Uenum {kernel/bpf/syscall.c +3
-1@@ -5987,8 +5987,10 @@ static int prog_stream_read(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 common_attrs;union bpf_attr attr;int err;
kernel/bpf/syscall.c static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size) · line 6012
Quoted 9 lines
kernel/bpf/syscall.c +6
-0@@ -6002,6 +6004,14 @@ 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 (&common_attrs,0 ,sizeof (common_attrs)); +if (cmd &BPF_COMMON_ATTRS ) { + cmd &= ~BPF_COMMON_ATTRS ; + size_common =min_t (u32, size_common,sizeof (common_attrs)); +if (uattr_common.user &©_from_bpfptr (&common_attrs, uattr_common, size_common)) +return -EFAULT ;
use bpf_check_uarg_tail_zero() for extra checks, just like we do for uattr
Quoted 5 lines
kernel/bpf/syscall.c +2
-0+ } + err =security_bpf (cmd, &attr, size, uattr.is_kernel);if (err <0 )return err;
[...]
Quoted 49 lines · Andrii Nakryiko
Quoted 48 lines · Leon Hwang
This patch extends 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 levelThese 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 <[email protected]>--- include/linux/syscalls.h | 3 ++- include/uapi/linux/bpf.h | 7 +++++++ kernel/bpf/syscall.c | 19 +++++++++++++++---- tools/include/uapi/linux/bpf.h | 7 +++++++ 4 files changed, 31 insertions(+), 5 deletions(-)include/linux/syscalls.h +2
-1@@ -933,7 +933,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 +0
-0@@ -1474,6 +1474,13 @@ struct bpf_stack_build_id { }; };
Quoted 12 lines
include/uapi/linux/bpf.h struct bpf_stack_build_id { · line 1483
Quoted 7 lines
include/uapi/linux/bpf.h +7
-0+struct bpf_common_attr { +__u64 log_buf; +__u32 log_size; +__u32 log_level; +}; + +#define BPF_COMMON_ATTRS (1 << 16)add this into enum
bpf_cmdafter__MAX_BPF_CMD(with a small comment about the purpose of this)? That will keep everything cmd-related in one place
Ack.
Quoted 23 lines
Quoted 23 lines
include/uapi/linux/bpf.h +0
-0#define BPF_OBJ_NAME_LEN 16Uenum {kernel/bpf/syscall.c +3
-1@@ -5987,8 +5987,10 @@ static int prog_stream_read(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 common_attrs;union bpf_attr attr;int err;@@ -6002,6 +6004,14 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size)if (copy_from_bpfptr (&attr, uattr, size) !=0 )return -EFAULT ;
Quoted 9 lines
kernel/bpf/syscall.c static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size) · line 6012
Quoted 6 lines
kernel/bpf/syscall.c +6
-0+memset (&common_attrs,0 ,sizeof (common_attrs)); +if (cmd &BPF_COMMON_ATTRS ) { + cmd &= ~BPF_COMMON_ATTRS ; + size_common =min_t (u32, size_common,sizeof (common_attrs)); +if (uattr_common.user &©_from_bpfptr (&common_attrs, uattr_common, size_common)) +return -EFAULT ;use
bpf_check_uarg_tail_zero()for extra checks, just like we do for uattr
Ack.
Thanks,
Leon
RFC PATCH v2 2/6 libbpf: Add support for extended bpf syscall
In reply to Leon HwangTo support the extended 'bpf()' syscall introduced in the previous commit, this patch adds the following APIs:
1. *Internal:*
* 'sys_bpf_extended()'
* 'sys_bpf_fd_extended()'
These wrap the raw 'syscall()' interface to support passing extended attributes.
2. *Exported:*
* 'probe_sys_bpf_extended()'
This function checks whether the running kernel supports the extended 'bpf()' syscall with common attributes.
--- tools/lib/bpf/bpf.c | 45 +++++++++++++++++++++++++++++++++ tools/lib/bpf/bpf.h | 1 + tools/lib/bpf/features.c | 8 ++++++ tools/lib/bpf/libbpf.map | 2 ++ tools/lib/bpf/libbpf_internal.h | 2 ++ 5 files changed, 58 insertions(+)
tools/lib/bpf/bpf.c +45 -0
@@ -69,6 +69,51 @@ static inline __u64 ptr_to_u64(const void *ptr)return (__u64 ) (unsigned long ) ptr; } +static inline int sys_bpf_extended (enum bpf_cmd cmd,union bpf_attr *attr, +unsigned int size, +struct bpf_common_attr *common_attrs, +unsigned int size_common) +{ + cmd = common_attrs ? cmd |BPF_COMMON_ATTRS : cmd & ~BPF_COMMON_ATTRS ; +return syscall (__NR_bpf, cmd, attr, size, common_attrs, size_common); +} + +static inline int sys_bpf_fd_extended (enum bpf_cmd cmd,union bpf_attr *attr, +unsigned int size, +struct bpf_common_attr *common_attrs, +unsigned int size_common) +{ +int fd; + + fd =sys_bpf_extended (cmd, attr, size, common_attrs, size_common); +return ensure_good_fd (fd); +} + +int probe_sys_bpf_extended (int token_fd) +{ + const size_t attr_sz = offsetofend(union bpf_attr, prog_token_fd); +struct bpf_common_attr common_attrs; +struct bpf_insn insns[] = { +BPF_MOV64_IMM (BPF_REG_0 ,0 ), +BPF_EXIT_INSN (), + }; +union bpf_attr attr; + +memset (&attr,0 , attr_sz); + attr.prog_type =BPF_PROG_TYPE_SOCKET_FILTER ; + attr.license =ptr_to_u64 ("GPL" ); + attr.insns =ptr_to_u64 (insns); + attr.insn_cnt = (__u32 )ARRAY_SIZE (insns); + attr.prog_token_fd = token_fd; +if (token_fd) + attr.prog_flags |=BPF_F_TOKEN_FD ; +libbpf_strlcpy (attr.prog_name,"libbpf_sysbpftest" ,sizeof (attr.prog_name)); +memset (&common_attrs,0 ,sizeof (common_attrs)); + +return sys_bpf_fd_extended (BPF_PROG_LOAD , &attr, attr_sz, &common_attrs, +sizeof (common_attrs)); +} +static inline int sys_bpf (enum bpf_cmd cmd,union bpf_attr *attr,unsigned int size) {
tools/lib/bpf/bpf.h +1 -0
@@ -35,6 +35,7 @@ extern "C" { #endif +LIBBPF_API int probe_sys_bpf_extended(int token_fd); LIBBPF_API int libbpf_set_memlock_rlim(size_t memlock_bytes); struct bpf_map_create_opts {
tools/lib/bpf/features.c +8 -0
@@ -507,6 +507,11 @@ static int probe_kern_arg_ctx_tag(int token_fd)return probe_fd (prog_fd); } +static int probe_kern_extended_syscall (int token_fd) +{ +return probe_fd (probe_sys_bpf_extended (token_fd)); +} +typedef int (*feature_probe_fn )(int /* token_fd */ );static struct kern_feature_cache feature_cache;
@@ -582,6 +587,9 @@ static struct kern_feature_desc { [FEAT_BTF_QMARK_DATASEC ] = {"BTF DATASEC names starting from '?'" , probe_kern_btf_qmark_datasec, }, + [FEAT_EXTENDED_SYSCALL ] = { +"Kernel supports extended syscall" , probe_kern_extended_syscall, + }, };bool feat_supported (struct kern_feature_cache *cache,enum kern_feature_id feat_id)
tools/lib/bpf/libbpf.map +2 -0
@@ -448,4 +448,6 @@ LIBBPF_1.6.0 { } LIBBPF_1.5.0; LIBBPF_1.7.0 { + global: + probe_sys_bpf_extended; } LIBBPF_1.6.0;
tools/lib/bpf/libbpf_internal.h +2 -0
@@ -380,6 +380,8 @@ enum kern_feature_id {FEAT_ARG_CTX_TAG ,/* Kernel supports '?' at the front of datasec names */ FEAT_BTF_QMARK_DATASEC , +/* Kernel supports extended syscall */ +FEAT_EXTENDED_SYSCALL , __FEAT_CNT, };
Signature · 1 line
2.50.1
Re: [RFC PATCH bpf-next v2 2/6] libbpf: Add support for extended bpf syscall
Quoted 17 lines · Leon Hwang
To support the extended '
bpf()' syscall introduced in the previous commit, this patch adds the following APIs:1. *Internal:*
* '
sys_bpf_extended()'
* 'sys_bpf_fd_extended()'
These wrap the raw 'syscall()' interface to support passing extended attributes.2. *Exported:*
* '
probe_sys_bpf_extended()'
This function checks whether the running kernel supports the extended 'bpf()' syscall with common attributes.
Quoted 9 lines
Signed-off-by Leon Hwang <[email protected]>--- tools/lib/bpf/bpf.c | 45 +++++++++++++++++++++++++++++++++ tools/lib/bpf/bpf.h | 1 + tools/lib/bpf/features.c | 8 ++++++ tools/lib/bpf/libbpf.map | 2 ++ tools/lib/bpf/libbpf_internal.h | 2 ++ 5 files changed, 58 insertions(+)
(ran out of time, will continue reviewing the rest of patches tomorrow, so please don't yet send new revision)
Quoted 8 lines
tools/lib/bpf/bpf.c +0
-0@@ -69,6 +69,51 @@ static inline __u64 ptr_to_u64(const void *ptr)return (__u64 ) (unsigned long ) ptr; }
tools/lib/bpf/bpf.c static inline __u64 ptr_to_u64(const void *ptr) · line 81
Quoted 10 lines
tools/lib/bpf/bpf.c +10
-0+static inline int sys_bpf_extended (enum bpf_cmd cmd,union bpf_attr *attr, +unsigned int size, +struct bpf_common_attr *common_attrs, +unsigned int size_common) +{ + cmd = common_attrs ? cmd |BPF_COMMON_ATTRS : cmd & ~BPF_COMMON_ATTRS ; +return syscall (__NR_bpf, cmd, attr, size, common_attrs, size_common); +} + +static inline int sys_bpf_fd_extended(enum bpf_cmd cmd, union bpf_attr *attr,
please shorten to sys_bpf_ext() and sys_bpf_ext_fd() (also note ext before fd)
Quoted 23 lines
tools/lib/bpf/bpf.c +23
-0+unsigned int size, + struct bpf_common_attr *common_attrs, + unsigned int size_common) +{ + int fd; + + fd =sys_bpf_extended (cmd, attr, size, common_attrs, size_common); +return ensure_good_fd (fd); +} + +int probe_sys_bpf_extended (int token_fd) +{ + const size_t attr_sz = offsetofend(union bpf_attr, prog_token_fd); +struct bpf_common_attr common_attrs; +struct bpf_insn insns[] = { +BPF_MOV64_IMM (BPF_REG_0 ,0 ), +BPF_EXIT_INSN (), + }; +union bpf_attr attr; + +memset (&attr,0 , attr_sz); + attr.prog_type =BPF_PROG_TYPE_SOCKET_FILTER ; + attr.license =ptr_to_u64 ("GPL" );
tools/lib/bpf/bpf.c static inline __u64 ptr_to_u64(const void *ptr) · line 114
Quoted 10 lines
tools/lib/bpf/bpf.c +10
-0+ attr.insns =ptr_to_u64 (insns); + attr.insn_cnt = (__u32 )ARRAY_SIZE (insns); + attr.prog_token_fd = token_fd; +if (token_fd) + attr.prog_flags |=BPF_F_TOKEN_FD ; +libbpf_strlcpy (attr.prog_name,"libbpf_sysbpftest" ,sizeof (attr.prog_name)); +memset (&common_attrs,0 ,sizeof (common_attrs)); + +return sys_bpf_fd_extended (BPF_PROG_LOAD , &attr, attr_sz, &common_attrs, +sizeof (common_attrs));
I think we can set up this feature detector such that we get -EINVAL due to BPF_COMMON_ATTRS not supported on old kernels, while -EFAULT on newer kernels due to NULL passed in common_attrs. This would be cheap and simple. Try it.
Quoted 10 lines
tools/lib/bpf/bpf.c +2
-0+} +static inline int sys_bpf (enum bpf_cmd cmd,union bpf_attr *attr,unsigned int size) {tools/lib/bpf/bpf.h +0
-0
tools/lib/bpf/bpf.h line 38
Quoted 4 lines
tools/lib/bpf/bpf.h +1
-0@@ -35,6 +35,7 @@extern "C" {#endif +LIBBPF_API int probe_sys_bpf_extended(int token_fd);
why adding this as a public UAPI?
Quoted 7 lines
tools/lib/bpf/bpf.h +0
-0LIBBPF_API int libbpf_set_memlock_rlim(size_t memlock_bytes);struct bpf_map_create_opts {tools/lib/bpf/features.c +0
-0
[...]
Re: [RFC PATCH bpf-next v2 2/6] libbpf: Add support for extended bpf syscall
Quoted 39 lines · Andrii Nakryiko
Quoted 17 lines · Leon Hwang
To support the extended '
bpf()' syscall introduced in the previous commit, this patch adds the following APIs:1. *Internal:*
* '
sys_bpf_extended()'
* 'sys_bpf_fd_extended()'
These wrap the raw 'syscall()' interface to support passing extended attributes.2. *Exported:*
* '
probe_sys_bpf_extended()'
This function checks whether the running kernel supports the extended 'bpf()' syscall with common attributes.Quoted 9 lines
Signed-off-by Leon Hwang <[email protected]>--- tools/lib/bpf/bpf.c | 45 +++++++++++++++++++++++++++++++++ tools/lib/bpf/bpf.h | 1 + tools/lib/bpf/features.c | 8 ++++++ tools/lib/bpf/libbpf.map | 2 ++ tools/lib/bpf/libbpf_internal.h | 2 ++ 5 files changed, 58 insertions(+)(ran out of time, will continue reviewing the rest of patches tomorrow, so please don't yet send new revision)
Quoted 8 lines
tools/lib/bpf/bpf.c +0
-0@@ -69,6 +69,51 @@ static inline __u64 ptr_to_u64(const void *ptr)return (__u64 ) (unsigned long ) ptr; }
Quoted 13 lines
tools/lib/bpf/bpf.c static inline __u64 ptr_to_u64(const void *ptr) · line 81
Quoted 10 lines
tools/lib/bpf/bpf.c +10
-0+static inline int sys_bpf_extended (enum bpf_cmd cmd,union bpf_attr *attr, +unsigned int size, +struct bpf_common_attr *common_attrs, +unsigned int size_common) +{ + cmd = common_attrs ? cmd |BPF_COMMON_ATTRS : cmd & ~BPF_COMMON_ATTRS ; +return syscall (__NR_bpf, cmd, attr, size, common_attrs, size_common); +} + +static inline int sys_bpf_fd_extended(enum bpf_cmd cmd, union bpf_attr *attr,please shorten to
sys_bpf_ext()andsys_bpf_ext_fd()(also note ext before fd)
The short ones look good to me.
Quoted 31 lines
Quoted 31 lines
tools/lib/bpf/bpf.c +30
-0+unsigned int size, + struct bpf_common_attr *common_attrs, + unsigned int size_common) +{ + int fd; + + fd =sys_bpf_extended (cmd, attr, size, common_attrs, size_common); +return ensure_good_fd (fd); +} + +int probe_sys_bpf_extended (int token_fd) +{ + const size_t attr_sz = offsetofend(union bpf_attr, prog_token_fd); +struct bpf_common_attr common_attrs; +struct bpf_insn insns[] = { +BPF_MOV64_IMM (BPF_REG_0 ,0 ), +BPF_EXIT_INSN (), + }; +union bpf_attr attr; + +memset (&attr,0 , attr_sz); + attr.prog_type =BPF_PROG_TYPE_SOCKET_FILTER ; + attr.license =ptr_to_u64 ("GPL" ); + attr.insns =ptr_to_u64 (insns); + attr.insn_cnt = (__u32 )ARRAY_SIZE (insns); + attr.prog_token_fd = token_fd; +if (token_fd) + attr.prog_flags |=BPF_F_TOKEN_FD ; +libbpf_strlcpy (attr.prog_name,"libbpf_sysbpftest" ,sizeof (attr.prog_name)); +memset (&common_attrs,0 ,sizeof (common_attrs));
Quoted 9 lines
tools/lib/bpf/bpf.c static inline __u64 ptr_to_u64(const void *ptr) · line 114
Quoted 3 lines
tools/lib/bpf/bpf.c +3
-0+ +return sys_bpf_fd_extended (BPF_PROG_LOAD , &attr, attr_sz, &common_attrs, +sizeof (common_attrs));I think we can set up this feature detector such that we get
-EINVALdue toBPF_COMMON_ATTRSnot supported on old kernels, while-EFAULTon newer kernels due to NULL passed incommon_attrs. This would be cheap and simple. Try it.
Let me give that a try.
Quoted 13 lines
Quoted 13 lines
tools/lib/bpf/bpf.c +2
-0+} +static inline int sys_bpf (enum bpf_cmd cmd,union bpf_attr *attr,unsigned int size) {tools/lib/bpf/bpf.h +0
-0@@ -35,6 +35,7 @@extern "C" {#endif
Quoted 4 lines
tools/lib/bpf/bpf.h line 38
Quoted 1 line
tools/lib/bpf/bpf.h +1
-0+LIBBPF_API int probe_sys_bpf_extended(int token_fd);why adding this as a public UAPI?
If we don’t mark it with LIBBPF_API, the build fails when compiling libbpf.
My intention here wasn’t to introduce a new public UAPI, but simply to provide a way for 'features.c' to probe whether the kernel supports the extended BPF syscall, without directly exposing 'sys_bpf_fd_extended()'.
Do you have a suggestion on how we can perform this probe without introducing a new LIBBPF_API symbol?
Thanks,
Leon
Re: [RFC PATCH bpf-next v2 2/6] libbpf: Add support for extended bpf syscall
Quoted 120 lines · Leon Hwang
Quoted 39 lines · Andrii Nakryiko
Quoted 17 lines · Leon Hwang
To support the extended '
bpf()' syscall introduced in the previous commit, this patch adds the following APIs:1. *Internal:*
* '
sys_bpf_extended()'
* 'sys_bpf_fd_extended()'
These wrap the raw 'syscall()' interface to support passing extended attributes.2. *Exported:*
* '
probe_sys_bpf_extended()'
This function checks whether the running kernel supports the extended 'bpf()' syscall with common attributes.Quoted 9 lines
Signed-off-by Leon Hwang <[email protected]>--- tools/lib/bpf/bpf.c | 45 +++++++++++++++++++++++++++++++++ tools/lib/bpf/bpf.h | 1 + tools/lib/bpf/features.c | 8 ++++++ tools/lib/bpf/libbpf.map | 2 ++ tools/lib/bpf/libbpf_internal.h | 2 ++ 5 files changed, 58 insertions(+)(ran out of time, will continue reviewing the rest of patches tomorrow, so please don't yet send new revision)
Quoted 8 lines
tools/lib/bpf/bpf.c +0
-0@@ -69,6 +69,51 @@ static inline __u64 ptr_to_u64(const void *ptr)return (__u64 ) (unsigned long ) ptr; }Quoted 13 lines
tools/lib/bpf/bpf.c static inline __u64 ptr_to_u64(const void *ptr) · line 81
Quoted 10 lines
tools/lib/bpf/bpf.c +10
-0+static inline int sys_bpf_extended (enum bpf_cmd cmd,union bpf_attr *attr, +unsigned int size, +struct bpf_common_attr *common_attrs, +unsigned int size_common) +{ + cmd = common_attrs ? cmd |BPF_COMMON_ATTRS : cmd & ~BPF_COMMON_ATTRS ; +return syscall (__NR_bpf, cmd, attr, size, common_attrs, size_common); +} + +static inline int sys_bpf_fd_extended(enum bpf_cmd cmd, union bpf_attr *attr,please shorten to
sys_bpf_ext()andsys_bpf_ext_fd()(also note ext before fd)The short ones look good to me.
Quoted 31 lines
Quoted 31 lines
tools/lib/bpf/bpf.c +30
-0+unsigned int size, + struct bpf_common_attr *common_attrs, + unsigned int size_common) +{ + int fd; + + fd =sys_bpf_extended (cmd, attr, size, common_attrs, size_common); +return ensure_good_fd (fd); +} + +int probe_sys_bpf_extended (int token_fd) +{ + const size_t attr_sz = offsetofend(union bpf_attr, prog_token_fd); +struct bpf_common_attr common_attrs; +struct bpf_insn insns[] = { +BPF_MOV64_IMM (BPF_REG_0 ,0 ), +BPF_EXIT_INSN (), + }; +union bpf_attr attr; + +memset (&attr,0 , attr_sz); + attr.prog_type =BPF_PROG_TYPE_SOCKET_FILTER ; + attr.license =ptr_to_u64 ("GPL" ); + attr.insns =ptr_to_u64 (insns); + attr.insn_cnt = (__u32 )ARRAY_SIZE (insns); + attr.prog_token_fd = token_fd; +if (token_fd) + attr.prog_flags |=BPF_F_TOKEN_FD ; +libbpf_strlcpy (attr.prog_name,"libbpf_sysbpftest" ,sizeof (attr.prog_name)); +memset (&common_attrs,0 ,sizeof (common_attrs));Quoted 9 lines
tools/lib/bpf/bpf.c static inline __u64 ptr_to_u64(const void *ptr) · line 114
Quoted 3 lines
tools/lib/bpf/bpf.c +3
-0+ +return sys_bpf_fd_extended (BPF_PROG_LOAD , &attr, attr_sz, &common_attrs, +sizeof (common_attrs));I think we can set up this feature detector such that we get
-EINVALdue toBPF_COMMON_ATTRSnot supported on old kernels, while-EFAULTon newer kernels due to NULL passed incommon_attrs. This would be cheap and simple. Try it.Let me give that a try.
Quoted 13 lines
Quoted 13 lines
tools/lib/bpf/bpf.c +2
-0+} +static inline int sys_bpf (enum bpf_cmd cmd,union bpf_attr *attr,unsigned int size) {tools/lib/bpf/bpf.h +0
-0@@ -35,6 +35,7 @@extern "C" {#endif Quoted 4 lines
tools/lib/bpf/bpf.h line 38
Quoted 1 line
tools/lib/bpf/bpf.h +1
-0+LIBBPF_API int probe_sys_bpf_extended(int token_fd);why adding this as a public UAPI?
Quoted 8 lines
If we don’t mark it with
LIBBPF_API, the build fails when compiling libbpf.My intention here wasn’t to introduce a new public UAPI, but simply to provide a way for '
features.c' to probe whether the kernel supports the extended BPF syscall, without directly exposing 'sys_bpf_fd_extended()'.Do you have a suggestion on how we can perform this probe without introducing a new
LIBBPF_APIsymbol?
see libbpf_internal.h, it's not the first non-UAPI global function we have in libbpf.
Just don't add this function to libbpf.map and it should be fine
Quoted 3 lines
Thanks,
Leon
RFC PATCH v2 3/6 bpf: Add common attr support for prog_load and btf_load
In reply to Leon HwangThe log buffer of common attributes would be confusing with the one in 'union bpf_attr' for BPF_PROG_LOAD and BPF_BTF_LOAD.
In order to clarify the usage of these two 'log_buf's, they both can be used for logging if:
* They are same, including 'log_buf', 'log_level' and 'log_size'.
* One of them is missing, then another one will be used for logging.
If they both have 'log_buf' but they are not same, a log message will be written to the log buffer of 'union bpf_attr'.
--- include/linux/bpf.h | 3 ++- include/linux/bpf_verifier.h | 2 +- include/linux/btf.h | 3 ++- kernel/bpf/btf.c | 12 +++++++----- kernel/bpf/log.c | 23 ++++++++++++++++++++++- kernel/bpf/syscall.c | 14 ++++++++------ kernel/bpf/verifier.c | 8 ++++---- 7 files changed, 46 insertions(+), 19 deletions(-)
include/linux/bpf.h +2 -1
@@ -2717,7 +2717,8 @@ 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); +int bpf_check (struct bpf_prog **fp,union bpf_attr *attr,bpfptr_t uattr,u32 uattr_size, +struct bpf_common_attr *common_attrs);#ifndef CONFIG_BPF_JIT_ALWAYS_ON void bpf_patch_call_args (struct bpf_insn *insn,u32 stack_depth);
include/linux/bpf_verifier.h +1 -1
@@ -864,7 +864,7 @@ __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, __printf(2, 3) void bpf_log(struct bpf_verifier_log *log,const char *fmt, ...);int bpf_vlog_init (struct bpf_verifier_log *log,u32 log_level, -char __user *log_buf,u32 log_size); +char __user *log_buf,u32 log_size,const struct bpf_common_attr *common_attrs);void bpf_vlog_reset (struct bpf_verifier_log *log,u64 new_pos);int bpf_vlog_finalize (struct bpf_verifier_log *log,u32 *log_size_actual);
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); +int btf_new_fd (const union bpf_attr *attr,bpfptr_t uattr,u32 uattr_sz, +const struct bpf_common_attr *common_attrs);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 -5
@@ -5771,7 +5771,8 @@ static int finalize_log(struct bpf_verifier_log *log, bpfptr_t uattr, u32 uattr_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,u32 uattr_size, +const struct bpf_common_attr *common_attrs) {bpfptr_t btf_data =make_bpfptr (attr->btf, uattr.is_kernel);char __user *log_ubuf =u64_to_user_ptr (attr->btf_log_buf);
@@ -5791,8 +5792,8 @@ 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->btf_log_level, log_ubuf, attr->btf_log_size, + common_attrs);if (err) goto errout_free;
@@ -8028,12 +8029,13 @@ 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,u32 uattr_size, +const struct bpf_common_attr *common_attrs) {struct btf *btf;int ret; - btf =btf_parse (attr, uattr, uattr_size); + btf =btf_parse (attr, uattr, uattr_size, common_attrs);if (IS_ERR (btf))return PTR_ERR (btf);
kernel/bpf/log.c +22 -1
@@ -29,12 +29,33 @@ static bool bpf_verifier_log_attr_valid(const struct bpf_verifier_log *log) }int bpf_vlog_init (struct bpf_verifier_log *log,u32 log_level, -char __user *log_buf,u32 log_size) +char __user *log_buf,u32 log_size,const struct bpf_common_attr *common_attrs) { +u32 log_true_size; +int err; + log->level = log_level; log->ubuf = log_buf; log->len_total = log_size; +if (log_buf && common_attrs && common_attrs->log_buf && + ((u64 ) log_buf != common_attrs->log_buf || log_level != common_attrs->log_level || + log_size != common_attrs->log_size)) { +if (!bpf_verifier_log_attr_valid (log)) +return -EINVAL ; +bpf_log (log,"Conflict log configs between bpf_attr and common_attr.\n" ); + err =bpf_vlog_finalize (log, &log_true_size); +if (err) +return err; +return -EINVAL ; + } + +if (!log_buf && common_attrs && common_attrs->log_buf) { + log->level = common_attrs->log_level; + log->ubuf =u64_to_user_ptr (common_attrs->log_buf); + log->len_total = common_attrs->log_size; + } +/* log attributes have to be sane */ if (!bpf_verifier_log_attr_valid (log))return -EINVAL ;
kernel/bpf/syscall.c +8 -6
@@ -2764,7 +2764,8 @@ static bool is_perfmon_prog_type(enum bpf_prog_type prog_type)/* last field in 'union bpf_attr' used by this command */ #define BPF_PROG_LOAD_LAST_FIELD fd_array_cnt -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,u32 uattr_size, +struct bpf_common_attr *common_attrs) {enum bpf_prog_type type = attr->prog_type;struct bpf_prog *prog, *dst_prog =NULL ;
@@ -2976,7 +2977,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, uattr_size, common_attrs);if (err <0 ) goto free_used_maps;
@@ -5292,7 +5293,8 @@ 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,__u32 uattr_size, +struct bpf_common_attr *common_attrs) {struct bpf_token *token =NULL ;
@@ -5319,7 +5321,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, uattr_size, common_attrs); }#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD fd_by_id_token_fd
@@ -6036,7 +6038,7 @@ 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); + err =bpf_prog_load (&attr, uattr, size, &common_attrs);break ;case BPF_OBJ_PIN : err =bpf_obj_pin (&attr);
@@ -6081,7 +6083,7 @@ 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); + err =bpf_btf_load (&attr, uattr, size, &common_attrs);break ;case BPF_BTF_GET_FD_BY_ID : err =bpf_btf_get_fd_by_id (&attr);
kernel/bpf/verifier.c +4 -4
@@ -24584,7 +24584,8 @@ static int compute_scc(struct bpf_verifier_env *env)return err; } -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,__u32 uattr_size, +struct bpf_common_attr *common_attrs) {u64 start_time =ktime_get_ns ();struct bpf_verifier_env *env;
@@ -24633,9 +24634,8 @@ 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,u64_to_user_ptr (attr->log_buf), + attr->log_size, common_attrs);if (ret) goto err_unlock;
Signature · 1 line
2.50.1
Re: [RFC PATCH bpf-next v2 3/6] bpf: Add common attr support for prog_load and btf_load
Quoted 10 lines · Leon Hwang
The log buffer of common attributes would be confusing with the one in 'union
bpf_attr' forBPF_PROG_LOADandBPF_BTF_LOAD.In order to clarify the usage of these two 'log_buf's, they both can be used for logging if:
* They are same, including '
log_buf', 'log_level' and 'log_size'.
* One of them is missing, then another one will be used for logging.
I agree with the logic above, but I'm not sure whether we need to plumb common_attrs all the way into bpf_vlog_init, tbh. There are only two commands that can have log specified through both bpf_attr and bpf_common_attrs. I'd have those two commands check and resolve the log buffer pointer, size and flags on their own (sure, a bit of duplicated logic, but we won't have any new command having to do that, so that's fine in my book).
And then I'd keep bpf_vlog_init completely unaware of common_attrs (which eventually have more stuff in it that's irrelevant to logging).
This seems cleaner than plumbing this through so deeply.
Quoted 3 lines
If they both have '
log_buf' but they are not same, a log message will be written to the log buffer of 'unionbpf_attr'.
Meh, whatever, this is unlikely user error, just error out with -EINVAL or something. Let's not invent "log here, but not here" logic.
Quoted 11 lines
Signed-off-by Leon Hwang <[email protected]>--- include/linux/bpf.h | 3 ++- include/linux/bpf_verifier.h | 2 +- include/linux/btf.h | 3 ++- kernel/bpf/btf.c | 12 +++++++----- kernel/bpf/log.c | 23 ++++++++++++++++++++++- kernel/bpf/syscall.c | 14 ++++++++------ kernel/bpf/verifier.c | 8 ++++---- 7 files changed, 46 insertions(+), 19 deletions(-)
[...]
Re: [RFC PATCH bpf-next v2 3/6] bpf: Add common attr support for prog_load and btf_load
Quoted 20 lines · Andrii Nakryiko
Quoted 10 lines · Leon Hwang
The log buffer of common attributes would be confusing with the one in 'union
bpf_attr' forBPF_PROG_LOADandBPF_BTF_LOAD.In order to clarify the usage of these two 'log_buf's, they both can be used for logging if:
* They are same, including '
log_buf', 'log_level' and 'log_size'.
* One of them is missing, then another one will be used for logging.I agree with the logic above, but I'm not sure whether we need to plumb
common_attrsall the way intobpf_vlog_init, tbh. There are only two commands that can have log specified through bothbpf_attrandbpf_common_attrs. I'd have those two commands check and resolve the log buffer pointer, size and flags on their own (sure, a bit of duplicated logic, but we won't have any new command having to do that, so that's fine in my book).
Quoted 3 lines
And then I'd keep
bpf_vlog_initcompletely unaware ofcommon_attrs(which eventually have more stuff in it that's irrelevant to logging).
To avoid modifying bpf_vlog_init directly, one option would be to introduce a new helper, e.g. bpf_vlog_init2 or bpf_vlog_init_with_cattrs, to handle the case with common_attrs.
This way, bpf_vlog_init_with_cattrs could be used for BPF_PROG_LOAD and BPF_BTF_LOAD, while the existing bpf_vlog_init remains unchanged and could be used for BPF_MAP_CREATE.
That would avoid duplicating the log handling logic, while also keeping the separation between the two cases clear.
Quoted 9 lines
This seems cleaner than plumbing this through so deeply.
Quoted 3 lines
If they both have '
log_buf' but they are not same, a log message will be written to the log buffer of 'unionbpf_attr'.Meh, whatever, this is unlikely user error, just error out with
-EINVALor something. Let's not invent "log here, but not here" logic.
In that case, we can return -EUSERS, as Alexei suggested earlier.
Thanks,
Leon
Re: [RFC PATCH bpf-next v2 3/6] bpf: Add common attr support for prog_load and btf_load
Quoted 32 lines · Leon Hwang
Quoted 20 lines · Andrii Nakryiko
Quoted 10 lines · Leon Hwang
The log buffer of common attributes would be confusing with the one in 'union
bpf_attr' forBPF_PROG_LOADandBPF_BTF_LOAD.In order to clarify the usage of these two 'log_buf's, they both can be used for logging if:
* They are same, including '
log_buf', 'log_level' and 'log_size'.
* One of them is missing, then another one will be used for logging.I agree with the logic above, but I'm not sure whether we need to plumb
common_attrsall the way intobpf_vlog_init, tbh. There are only two commands that can have log specified through bothbpf_attrandbpf_common_attrs. I'd have those two commands check and resolve the log buffer pointer, size and flags on their own (sure, a bit of duplicated logic, but we won't have any new command having to do that, so that's fine in my book).Quoted 3 lines
And then I'd keep
bpf_vlog_initcompletely unaware ofcommon_attrs(which eventually have more stuff in it that's irrelevant to logging).To avoid modifying
bpf_vlog_initdirectly, one option would be to introduce a new helper, e.g.bpf_vlog_init2orbpf_vlog_init_with_cattrs, to handle the case withcommon_attrs.
Quoted 6 lines
This way,
bpf_vlog_init_with_cattrscould be used forBPF_PROG_LOADandBPF_BTF_LOAD, while the existingbpf_vlog_initremains unchanged and could be used forBPF_MAP_CREATE.That would avoid duplicating the log handling logic, while also keeping the separation between the two cases clear.
We are talking about duplicating 1-2 if conditions. One-time thing, we won't be having any more commands going forward that will allow specifying log in two different ways. Let's not add a zoo of bpf_vlog_xxx constructors
Quoted 7 lines
Quoted 7 lines
This seems cleaner than plumbing this through so deeply.
Quoted 3 lines
If they both have '
log_buf' but they are not same, a log message will be written to the log buffer of 'unionbpf_attr'.
Quoted 6 lines
Quoted 3 lines
Meh, whatever, this is unlikely user error, just error out with
-EINVALor something. Let's not invent "log here, but not here" logic.In that case, we can return
-EUSERS, as Alexei suggested earlier.
TIL about -EUSERS, works for me
Quoted 2 lines
Thanks,
Leon
RFC PATCH v2 4/6 bpf: Add common attr support for map_create
In reply to Leon HwangCurrently, many 'BPF_MAP_CREATE' failures return '-EINVAL' without providing any explanation to user space.
With the extended BPF syscall support introduced in the previous patch, detailed error messages can now be reported. This allows users to understand the specific reason for a failed map creation, rather than just receiving a generic '-EINVAL'.
--- kernel/bpf/syscall.c | 82 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 63 insertions(+), 19 deletions(-)
kernel/bpf/syscall.c +63 -19
@@ -1340,12 +1340,13 @@ static bool bpf_net_capable(void)#define BPF_MAP_CREATE_LAST_FIELD map_token_fd/* called via syscall */ -static int map_create (union bpf_attr *attr,bool kernel) +static int map_create (union bpf_attr *attr,bool kernel,struct bpf_common_attr *common_attrs) { +u32 map_type = attr->map_type, log_true_size; +struct bpf_verifier_log *log =NULL ;const struct bpf_map_ops *ops;struct bpf_token *token =NULL ;int numa_node =bpf_map_attr_numa_node (attr); -u32 map_type = attr->map_type;struct bpf_map *map;bool token_flag;int f_flags;
@@ -1355,6 +1356,18 @@ static int map_create(union bpf_attr *attr, bool kernel)if (err)return -EINVAL ; +if (common_attrs->log_buf) { + log =kvzalloc (sizeof (*log),GFP_KERNEL ); +if (!log) +return -ENOMEM ; + err =bpf_vlog_init (log,BPF_LOG_FIXED ,u64_to_user_ptr (common_attrs->log_buf), + common_attrs->log_size,NULL ); +if (err) { +kvfree (log); +return err; + } + } +/* check BPF_F_TOKEN_FD flag, remember if it's set, and then clear it * to avoid per-map type checks tripping on unknown flag */
@@ -1363,16 +1376,24 @@ static int map_create(union bpf_attr *attr, bool kernel)if (attr->btf_vmlinux_value_type_id) {if (attr->map_type !=BPF_MAP_TYPE_STRUCT_OPS || - attr->btf_key_type_id || attr->btf_value_type_id) -return -EINVAL ; + attr->btf_key_type_id || attr->btf_value_type_id) { +bpf_log (log,"Invalid use of btf_vmlinux_value_type_id.\n" ); + err = -EINVAL ; + goto put_token; + } }else if (attr->btf_key_type_id && !attr->btf_value_type_id) { -return -EINVAL ; +bpf_log (log,"Invalid btf_value_type_id.\n" ); + err = -EINVAL ; + goto put_token; }if (attr->map_type !=BPF_MAP_TYPE_BLOOM_FILTER && attr->map_type !=BPF_MAP_TYPE_ARENA && - attr->map_extra !=0 ) -return -EINVAL ; + attr->map_extra !=0 ) { +bpf_log (log,"Invalid map_extra.\n" ); + err = -EINVAL ; + goto put_token; + } f_flags =bpf_get_file_flag (attr->map_flags);if (f_flags <0 )
@@ -1380,17 +1401,26 @@ static int map_create(union bpf_attr *attr, bool kernel)if (numa_node !=NUMA_NO_NODE && ((unsigned int )numa_node >= nr_node_ids || - !node_online (numa_node))) -return -EINVAL ; + !node_online (numa_node))) { +bpf_log (log,"Invalid or offline numa_node.\n" ); + err = -EINVAL ; + goto put_token; + }/* find map type and init map: hashtable vs rbtree vs bloom vs ... */ map_type = attr->map_type; -if (map_type >=ARRAY_SIZE (bpf_map_types)) -return -EINVAL ; +if (map_type >=ARRAY_SIZE (bpf_map_types)) { +bpf_log (log,"Invalid map_type.\n" ); + err = -EINVAL ; + goto put_token; + } map_type =array_index_nospec (map_type,ARRAY_SIZE (bpf_map_types)); ops = bpf_map_types[map_type]; -if (!ops) -return -EINVAL ; +if (!ops) { +bpf_log (log,"Invalid map_type.\n" ); + err = -EINVAL ; + goto put_token; + }if (ops->map_alloc_check) { err = ops->map_alloc_check (attr);
@@ -1399,13 +1429,20 @@ static int map_create(union bpf_attr *attr, bool kernel) }if (attr->map_ifindex) ops = &bpf_map_offload_ops; -if (!ops->map_mem_usage) -return -EINVAL ; +if (!ops->map_mem_usage) { +bpf_log (log,"map_mem_usage is required.\n" ); + err = -EINVAL ; + goto put_token; + }if (token_flag) { token =bpf_token_get_from_fd (attr->map_token_fd); -if (IS_ERR (token)) -return PTR_ERR (token); +if (IS_ERR (token)) { +bpf_log (log,"Invalid map_token_fd.\n" ); + err =PTR_ERR (token); + token =NULL ; + goto put_token; + }/* if current token doesn't grant map creation permissions, * then we can't use this token, so ignore it and rely on
@@ -1487,8 +1524,10 @@ static int map_create(union bpf_attr *attr, bool kernel) err =bpf_obj_name_cpy (map->name, attr->map_name,sizeof (attr->map_name)); -if (err <0 ) +if (err <0 ) { +bpf_log (log,"Invalid map_name.\n" ); goto free_map; + }preempt_disable (); map->cookie =gen_cookie_next (&bpf_map_cookie);
@@ -1511,6 +1550,7 @@ static int map_create(union bpf_attr *attr, bool kernel) btf =btf_get_by_fd (attr->btf_fd);if (IS_ERR (btf)) { +bpf_log (log,"Invalid btf_fd.\n" ); err =PTR_ERR (btf); goto free_map; }
@@ -1565,6 +1605,10 @@ static int map_create(union bpf_attr *attr, bool kernel)bpf_map_free (map); put_token:bpf_token_put (token); +if (err && log) + (void )bpf_vlog_finalize (log, &log_true_size); +if (log) +kvfree (log);return err; }
@@ -6020,7 +6064,7 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size,switch (cmd) {case BPF_MAP_CREATE : - err =map_create (&attr, uattr.is_kernel); + err =map_create (&attr, uattr.is_kernel, &common_attrs);break ;case BPF_MAP_LOOKUP_ELEM : err =map_lookup_elem (&attr);
Signature · 1 line
2.50.1
Re: [RFC PATCH bpf-next v2 4/6] bpf: Add common attr support for map_create
Quoted 39 lines · Leon Hwang
Currently, many '
BPF_MAP_CREATE' failures return '-EINVAL' without providing any explanation to user space.With the extended BPF syscall support introduced in the previous patch, detailed error messages can now be reported. This allows users to understand the specific reason for a failed map creation, rather than just receiving a generic '
-EINVAL'.Signed-off-by Leon Hwang <[email protected]>--- kernel/bpf/syscall.c | 82 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 63 insertions(+), 19 deletions(-)kernel/bpf/syscall.c +4
-2@@ -1340,12 +1340,13 @@ static bool bpf_net_capable(void)#define BPF_MAP_CREATE_LAST_FIELD map_token_fd/* called via syscall */ -static int map_create (union bpf_attr *attr,bool kernel) +static int map_create (union bpf_attr *attr,bool kernel,struct bpf_common_attr *common_attrs) { +u32 map_type = attr->map_type, log_true_size; +struct bpf_verifier_log *log =NULL ;const struct bpf_map_ops *ops;struct bpf_token *token =NULL ;int numa_node =bpf_map_attr_numa_node (attr); -u32 map_type = attr->map_type;struct bpf_map *map;bool token_flag;int f_flags;@@ -1355,6 +1356,18 @@ static int map_create(union bpf_attr *attr, bool kernel)if (err)return -EINVAL ; +if (common_attrs->log_buf) {
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bool kernel) · line 1369
Quoted 10 lines
kernel/bpf/syscall.c +10
-0+ log =kvzalloc (sizeof (*log),GFP_KERNEL ); +if (!log) +return -ENOMEM ; + err =bpf_vlog_init (log,BPF_LOG_FIXED ,u64_to_user_ptr (common_attrs->log_buf), + common_attrs->log_size,NULL ); +if (err) { +kvfree (log); +return err; + } + }
what if we keep bpf_verifier_log on stack? It's 1KB, should be still fine to be on kernel stack, no?
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bool kernel) · line 1380
Quoted 12 lines
kernel/bpf/syscall.c +3
-2+/* check BPF_F_TOKEN_FD flag, remember if it's set, and then clear it * to avoid per-map type checks tripping on unknown flag */ @@ -1363,16 +1376,24 @@ static int map_create(union bpf_attr *attr, bool kernel)if (attr->btf_vmlinux_value_type_id) {if (attr->map_type !=BPF_MAP_TYPE_STRUCT_OPS || - attr->btf_key_type_id || attr->btf_value_type_id) -return -EINVAL ; + attr->btf_key_type_id || attr->btf_value_type_id) { +bpf_log (log,"Invalid use of btf_vmlinux_value_type_id.\n" );
I don't know how far we want to go here, but I'd split the original check into map type check and key_type/value_type check, and log a bit more meaningful error:
a) btf_vmlinux_value_type_id can only be used with struct_ops maps.
and
b) btf_vmlinux_value_type_id is mutually exclusive with btf_key_type_id and btf_value_type_id
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bool kernel) · line 1382
Quoted 2 lines
kernel/bpf/syscall.c +2
-0+ err = -EINVAL ; + goto put_token;
there is no token just yet, add new label for finalizing log?
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bool kernel) · line 1387
Quoted 6 lines
kernel/bpf/syscall.c +4
-1+ } }else if (attr->btf_key_type_id && !attr->btf_value_type_id) { -return -EINVAL ; +bpf_log (log,"Invalid btf_value_type_id.\n" ); + err = -EINVAL ; + goto put_token;
ditto about token
Quoted 15 lines
kernel/bpf/syscall.c +5
-2}if (attr->map_type !=BPF_MAP_TYPE_BLOOM_FILTER && attr->map_type !=BPF_MAP_TYPE_ARENA && - attr->map_extra !=0 ) -return -EINVAL ; + attr->map_extra !=0 ) { +bpf_log (log,"Invalid map_extra.\n" ); + err = -EINVAL ; + goto put_token; + } f_flags =bpf_get_file_flag (attr->map_flags);if (f_flags <0 )
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bool kernel) · line 1405
Quoted 7 lines
kernel/bpf/syscall.c +2
-2@@ -1380,17 +1401,26 @@ static int map_create(union bpf_attr *attr, bool kernel)if (numa_node !=NUMA_NO_NODE && ((unsigned int )numa_node >= nr_node_ids || - !node_online (numa_node))) -return -EINVAL ; + !node_online (numa_node))) { +bpf_log (log,"Invalid or offline numa_node.\n" );
nit: just "invalid numa_node" ?
Quoted 26 lines
kernel/bpf/syscall.c +13
-4+ err = -EINVAL ; + goto put_token; + }/* find map type and init map: hashtable vs rbtree vs bloom vs ... */ map_type = attr->map_type; -if (map_type >=ARRAY_SIZE (bpf_map_types)) -return -EINVAL ; +if (map_type >=ARRAY_SIZE (bpf_map_types)) { +bpf_log (log,"Invalid map_type.\n" ); + err = -EINVAL ; + goto put_token; + } map_type =array_index_nospec (map_type,ARRAY_SIZE (bpf_map_types)); ops = bpf_map_types[map_type]; -if (!ops) -return -EINVAL ; +if (!ops) { +bpf_log (log,"Invalid map_type.\n" ); + err = -EINVAL ; + goto put_token; + }if (ops->map_alloc_check) { err = ops->map_alloc_check (attr);
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bool kernel) · line 1433
Quoted 7 lines
kernel/bpf/syscall.c +2
-2@@ -1399,13 +1429,20 @@ static int map_create(union bpf_attr *attr, bool kernel) }if (attr->map_ifindex) ops = &bpf_map_offload_ops; -if (!ops->map_mem_usage) -return -EINVAL ; +if (!ops->map_mem_usage) { +bpf_log (log,"map_mem_usage is required.\n" );
this is kernel bug, actually, so let's log "bug: " prefix? same above with the second "Invalid map_type." message?
and actually, given these are kernel bugs and shouldn't happen, I wonder if we should log anything here at all?
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bool kernel) · line 1444
Quoted 13 lines
kernel/bpf/syscall.c +8
-2+ err = -EINVAL ; + goto put_token; + }if (token_flag) { token =bpf_token_get_from_fd (attr->map_token_fd); -if (IS_ERR (token)) -return PTR_ERR (token); +if (IS_ERR (token)) { +bpf_log (log,"Invalid map_token_fd.\n" ); + err =PTR_ERR (token); + token =NULL ; + goto put_token;
ditto, no token
Quoted 25 lines
kernel/bpf/syscall.c +5
-1+ }/* if current token doesn't grant map creation permissions, * then we can't use this token, so ignore it and rely on @@ -1487,8 +1524,10 @@ static int map_create(union bpf_attr *attr, bool kernel) err =bpf_obj_name_cpy (map->name, attr->map_name,sizeof (attr->map_name)); -if (err <0 ) +if (err <0 ) { +bpf_log (log,"Invalid map_name.\n" ); goto free_map; + }preempt_disable (); map->cookie =gen_cookie_next (&bpf_map_cookie);@@ -1511,6 +1550,7 @@ static int map_create(union bpf_attr *attr, bool kernel) btf =btf_get_by_fd (attr->btf_fd);if (IS_ERR (btf)) { +bpf_log (log,"Invalid btf_fd.\n" ); err =PTR_ERR (btf); goto free_map; }
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bool kernel) · line 1609
Quoted 5 lines
kernel/bpf/syscall.c +2
-0@@ -1565,6 +1605,10 @@ static int map_create(union bpf_attr *attr, bool kernel)bpf_map_free (map); put_token:bpf_token_put (token); +if (err && log) + (void )bpf_vlog_finalize (log, &log_true_size);
so we'll just drop this size on the floor and never report it to the user?
a) let's either teach bpf_vlog_finalize that log_true_size pointer can be NULL (and optional),
or b) let's report it back to user through that same commont_attrs struct, just like we do it for verifier and btf logs?
Also, what if complicating error handling with this goto jumping, can we make use of __cleanup() attribute to do this automatically? Then we'd just allocate (or see above, maybe just init on the stack) log struct, and declare it with cleanup callback that will do this vlog_finalize and, maybe, report back the log actual size?
Quoted 17 lines
kernel/bpf/syscall.c +3
-2+if (log) +kvfree (log);return err; }@@ -6020,7 +6064,7 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size,switch (cmd) {case BPF_MAP_CREATE : - err =map_create (&attr, uattr.is_kernel); + err =map_create (&attr, uattr.is_kernel, &common_attrs);break ;case BPF_MAP_LOOKUP_ELEM : err =map_lookup_elem (&attr); --2.50.1
Re: [RFC PATCH bpf-next v2 4/6] bpf: Add common attr support for map_create
Quoted 40 lines · Andrii Nakryiko
Quoted 38 lines · Leon Hwang
Currently, many '
BPF_MAP_CREATE' failures return '-EINVAL' without providing any explanation to user space.With the extended BPF syscall support introduced in the previous patch, detailed error messages can now be reported. This allows users to understand the specific reason for a failed map creation, rather than just receiving a generic '
-EINVAL'.Signed-off-by Leon Hwang <[email protected]>--- kernel/bpf/syscall.c | 82 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 63 insertions(+), 19 deletions(-)kernel/bpf/syscall.c +3
-2@@ -1340,12 +1340,13 @@ static bool bpf_net_capable(void)#define BPF_MAP_CREATE_LAST_FIELD map_token_fd/* called via syscall */ -static int map_create (union bpf_attr *attr,bool kernel) +static int map_create (union bpf_attr *attr,bool kernel,struct bpf_common_attr *common_attrs) { +u32 map_type = attr->map_type, log_true_size; +struct bpf_verifier_log *log =NULL ;const struct bpf_map_ops *ops;struct bpf_token *token =NULL ;int numa_node =bpf_map_attr_numa_node (attr); -u32 map_type = attr->map_type;struct bpf_map *map;bool token_flag;int f_flags;@@ -1355,6 +1356,18 @@ static int map_create(union bpf_attr *attr, bool kernel)if (err)return -EINVAL ;
Quoted 14 lines
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bool kernel) · line 1369
Quoted 11 lines
kernel/bpf/syscall.c +11
-0+if (common_attrs->log_buf) { + log =kvzalloc (sizeof (*log),GFP_KERNEL ); +if (!log) +return -ENOMEM ; + err =bpf_vlog_init (log,BPF_LOG_FIXED ,u64_to_user_ptr (common_attrs->log_buf), + common_attrs->log_size,NULL ); +if (err) { +kvfree (log); +return err; + } + }what if we keep
bpf_verifier_logon stack? It's 1KB, should be still fine to be on kernel stack, no?
1k is a lot. I'm pretty nervous about stack usage. kmalloc is cheap. Let's use it. I'd drop 'v' part. There is no way it will fallback to valloc() even if we double BPF_VERIFIER_TMP_LOG_SIZE in the future.
Re: [RFC PATCH bpf-next v2 4/6] bpf: Add common attr support for map_create
Quoted 5 lines · Alexei Starovoitov
Quoted 3 lines · Andrii Nakryiko
Quoted 1 line · Leon Hwang
[...]
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
kernel/bpf/syscall.c +11
-0+if (common_attrs->log_buf) { + log =kvzalloc (sizeof (*log),GFP_KERNEL ); +if (!log) +return -ENOMEM ; + err =bpf_vlog_init (log,BPF_LOG_FIXED ,u64_to_user_ptr (common_attrs->log_buf), + common_attrs->log_size,NULL ); +if (err) { +kvfree (log); +return err; + } + }
Quoted 6 lines
Quoted 2 lines
what if we keep
bpf_verifier_logon stack? It's 1KB, should be still fine to be on kernel stack, no?1k is a lot. I'm pretty nervous about stack usage. kmalloc is cheap. Let's use it. I'd drop 'v' part. There is no way it will fallback to
valloc()even if we doubleBPF_VERIFIER_TMP_LOG_SIZEin the future.
I'll drop 'v' part in the next revision.
Thanks,
Leon
Re: [RFC PATCH bpf-next v2 4/6] bpf: Add common attr support for map_create
In reply to Andrii NakryikoQuoted 39 lines · Andrii Nakryiko
Quoted 38 lines · Leon Hwang
Currently, many '
BPF_MAP_CREATE' failures return '-EINVAL' without providing any explanation to user space.With the extended BPF syscall support introduced in the previous patch, detailed error messages can now be reported. This allows users to understand the specific reason for a failed map creation, rather than just receiving a generic '
-EINVAL'.Signed-off-by Leon Hwang <[email protected]>--- kernel/bpf/syscall.c | 82 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 63 insertions(+), 19 deletions(-)kernel/bpf/syscall.c +3
-2@@ -1340,12 +1340,13 @@ static bool bpf_net_capable(void)#define BPF_MAP_CREATE_LAST_FIELD map_token_fd/* called via syscall */ -static int map_create (union bpf_attr *attr,bool kernel) +static int map_create (union bpf_attr *attr,bool kernel,struct bpf_common_attr *common_attrs) { +u32 map_type = attr->map_type, log_true_size; +struct bpf_verifier_log *log =NULL ;const struct bpf_map_ops *ops;struct bpf_token *token =NULL ;int numa_node =bpf_map_attr_numa_node (attr); -u32 map_type = attr->map_type;struct bpf_map *map;bool token_flag;int f_flags;@@ -1355,6 +1356,18 @@ static int map_create(union bpf_attr *attr, bool kernel)if (err)return -EINVAL ;
Quoted 15 lines
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bool kernel) · line 1369
Quoted 11 lines
kernel/bpf/syscall.c +11
-0+if (common_attrs->log_buf) { + log =kvzalloc (sizeof (*log),GFP_KERNEL ); +if (!log) +return -ENOMEM ; + err =bpf_vlog_init (log,BPF_LOG_FIXED ,u64_to_user_ptr (common_attrs->log_buf), + common_attrs->log_size,NULL ); +if (err) { +kvfree (log); +return err; + } + }what if we keep
bpf_verifier_logon stack? It's 1KB, should be still fine to be on kernel stack, no?
I'm going to follow Alexei's suggestion.
Quoted 18 lines
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bool kernel) · line 1380
Quoted 13 lines
kernel/bpf/syscall.c +3
-2+/* check BPF_F_TOKEN_FD flag, remember if it's set, and then clear it * to avoid per-map type checks tripping on unknown flag */ @@ -1363,16 +1376,24 @@ static int map_create(union bpf_attr *attr, bool kernel)if (attr->btf_vmlinux_value_type_id) {if (attr->map_type !=BPF_MAP_TYPE_STRUCT_OPS || - attr->btf_key_type_id || attr->btf_value_type_id) -return -EINVAL ; + attr->btf_key_type_id || attr->btf_value_type_id) { +bpf_log (log,"Invalid use of btf_vmlinux_value_type_id.\n" );I don't know how far we want to go here, but I'd split the original check into map type check and key_type/value_type check, and log a bit more meaningful error:
Quoted 7 lines
a)
btf_vmlinux_value_type_idcan only be used withstruct_opsmaps.and
b)
btf_vmlinux_value_type_idis mutually exclusive withbtf_key_type_idandbtf_value_type_id
Sure.
It would be better to separate it like this.
Quoted 5 lines
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bool kernel) · line 1382
Quoted 2 lines
kernel/bpf/syscall.c +2
-0+ err = -EINVAL ; + goto put_token;there is no token just yet, add new label for finalizing log?
Ack.
Quoted 9 lines
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bool kernel) · line 1387
Quoted 6 lines
kernel/bpf/syscall.c +4
-1+ } }else if (attr->btf_key_type_id && !attr->btf_value_type_id) { -return -EINVAL ; +bpf_log (log,"Invalid btf_value_type_id.\n" ); + err = -EINVAL ; + goto put_token;ditto about token
Ack.
Quoted 10 lines
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bool kernel) · line 1395
Quoted 10 lines
kernel/bpf/syscall.c +4
-2}if (attr->map_type !=BPF_MAP_TYPE_BLOOM_FILTER && attr->map_type !=BPF_MAP_TYPE_ARENA && - attr->map_extra !=0 ) -return -EINVAL ; + attr->map_extra !=0 ) { +bpf_log (log,"Invalid map_extra.\n" ); + err = -EINVAL ; + goto put_token;
It'll be changed to the new label.
Quoted 6 lines
Quoted 6 lines
kernel/bpf/syscall.c +1
-0+ } f_flags =bpf_get_file_flag (attr->map_flags);if (f_flags <0 )@@ -1380,17 +1401,26 @@ static int map_create(union bpf_attr *attr, bool kernel)
Quoted 10 lines
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bool kernel) · line 1405
Quoted 6 lines
kernel/bpf/syscall.c +2
-2if (numa_node !=NUMA_NO_NODE && ((unsigned int )numa_node >= nr_node_ids || - !node_online (numa_node))) -return -EINVAL ; + !node_online (numa_node))) { +bpf_log (log,"Invalid or offline numa_node.\n" );nit: just "invalid
numa_node" ?
Ack.
Quoted 2 lines
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bool kernel) · line 1407
Quoted 2 lines
kernel/bpf/syscall.c +2
-0+ err = -EINVAL ; + goto put_token;
It'll be changed to the new label.
Quoted 10 lines
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bool kernel) · line 1415
Quoted 10 lines
kernel/bpf/syscall.c +5
-2+ }/* find map type and init map: hashtable vs rbtree vs bloom vs ... */ map_type = attr->map_type; -if (map_type >=ARRAY_SIZE (bpf_map_types)) -return -EINVAL ; +if (map_type >=ARRAY_SIZE (bpf_map_types)) { +bpf_log (log,"Invalid map_type.\n" ); + err = -EINVAL ; + goto put_token;
It'll be changed to the new label.
Quoted 9 lines
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bool kernel) · line 1422
Quoted 9 lines
kernel/bpf/syscall.c +5
-2+ } map_type =array_index_nospec (map_type,ARRAY_SIZE (bpf_map_types)); ops = bpf_map_types[map_type]; -if (!ops) -return -EINVAL ; +if (!ops) { +bpf_log (log,"Invalid map_type.\n" ); + err = -EINVAL ; + goto put_token;
It'll be changed to the new label.
Quoted 17 lines
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bool kernel) · line 1433
Quoted 12 lines
kernel/bpf/syscall.c +3
-2+ }if (ops->map_alloc_check) { err = ops->map_alloc_check (attr);@@ -1399,13 +1429,20 @@ static int map_create(union bpf_attr *attr, bool kernel) }if (attr->map_ifindex) ops = &bpf_map_offload_ops; -if (!ops->map_mem_usage) -return -EINVAL ; +if (!ops->map_mem_usage) { +bpf_log (log,"map_mem_usage is required.\n" );this is kernel bug, actually, so let's log "bug: " prefix? same above with the second "Invalid
map_type." message?
Quoted 3 lines
and actually, given these are kernel bugs and shouldn't happen, I wonder if we should log anything here at all?
As it's a kernel bug if no map_mem_usage, would it be better to WARN_ONCE here instead of reporting a log?
Quoted 2 lines
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bool kernel) · line 1435
Quoted 2 lines
kernel/bpf/syscall.c +2
-0+ err = -EINVAL ; + goto put_token;
It'll be changed to the new label.
Quoted 12 lines
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bool kernel) · line 1444
Quoted 11 lines
kernel/bpf/syscall.c +6
-2+ }if (token_flag) { token =bpf_token_get_from_fd (attr->map_token_fd); -if (IS_ERR (token)) -return PTR_ERR (token); +if (IS_ERR (token)) { +bpf_log (log,"Invalid map_token_fd.\n" ); + err =PTR_ERR (token); + token =NULL ; + goto put_token;
Quoted 2 lines
ditto, no token
It'll be changed to the new label.
Quoted 34 lines
Quoted 25 lines
kernel/bpf/syscall.c +5
-1+ }/* if current token doesn't grant map creation permissions, * then we can't use this token, so ignore it and rely on @@ -1487,8 +1524,10 @@ static int map_create(union bpf_attr *attr, bool kernel) err =bpf_obj_name_cpy (map->name, attr->map_name,sizeof (attr->map_name)); -if (err <0 ) +if (err <0 ) { +bpf_log (log,"Invalid map_name.\n" ); goto free_map; + }preempt_disable (); map->cookie =gen_cookie_next (&bpf_map_cookie);@@ -1511,6 +1550,7 @@ static int map_create(union bpf_attr *attr, bool kernel) btf =btf_get_by_fd (attr->btf_fd);if (IS_ERR (btf)) { +bpf_log (log,"Invalid btf_fd.\n" ); err =PTR_ERR (btf); goto free_map; }kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bool kernel) · line 1609
Quoted 5 lines
kernel/bpf/syscall.c +2
-0@@ -1565,6 +1605,10 @@ static int map_create(union bpf_attr *attr, bool kernel)bpf_map_free (map); put_token:bpf_token_put (token); +if (err && log) + (void )bpf_vlog_finalize (log, &log_true_size);so we'll just drop this size on the floor and never report it to the user?
Quoted 5 lines
a) let's either teach
bpf_vlog_finalizethatlog_true_sizepointer can be NULL (and optional),
or b) let's report it back to user through that samecommont_attrsstruct, just like we do it for verifier and btf logs?
Reporting it back to user looks better to me.
Quoted 7 lines
Also, what if complicating error handling with this goto jumping, can we make use of
__cleanup()attribute to do this automatically? Then we'd just allocate (or see above, maybe just init on the stack) log struct, and declare it with cleanup callback that will do thisvlog_finalizeand, maybe, report back the log actual size?
It does seem feasible to simplify error handling with __cleanup().
Instead of initializing log directly on the stack, we could introduce a small wrapper:
struct bpf_log_wrapper {
struct bpf_verifier_log *log;
};with a destructor:
static void bpf_log_wrapper_destructor(struct bpf_log_wrapper *w)
{
u32 log_true_size;
if (w->log) {
(void) bpf_vlog_finalize(w->log, &log_true_size);
kfree(w->log);
}
}In this demo snippet I skipped handling log_true_size, but in practice it should be dealt with properly.
Then, for example, in map_create():
struct bpf_log_wrapper log_wrapper __cleanup(bpf_log_wrapper_destructor) = {};
if (common_attrs->log_buf) {
log_wrapper.log = kzalloc(...);
}I’ll give this approach a try in the next revision.
Thanks,
Leon
Eduard Zingerman
maintainerRe: [RFC PATCH bpf-next v2 4/6] bpf: Add common attr support for map_create
In reply to Leon HwangOn Fri, 2025-09-12 at 00:33 +0800, Leon Hwang wrote:
[...]
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bool kernel) · line 1364
Quoted 10 lines
kernel/bpf/syscall.c +6
-0@@ -1355,6 +1356,18 @@ static int map_create(union bpf_attr *attr, bool kernel)if (err)return -EINVAL ; +if (common_attrs->log_buf) { + log =kvzalloc (sizeof (*log),GFP_KERNEL ); +if (!log) +return -ENOMEM ; + err =bpf_vlog_init (log,BPF_LOG_FIXED ,u64_to_user_ptr (common_attrs->log_buf), + common_attrs->log_size,NULL );
Maybe use common_attrs->log_level instead of BPF_LOG_FIXED? Just for consistent behavior with program and btf load operations.
Quoted 9 lines
kernel/bpf/syscall.c +6
-0+if (err) { +kvfree (log); +return err; + } + } +/* check BPF_F_TOKEN_FD flag, remember if it's set, and then clear it * to avoid per-map type checks tripping on unknown flag */
[...]
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bool kernel) · line 1613
Quoted 10 lines
kernel/bpf/syscall.c +4
-0@@ -1565,6 +1605,10 @@ static int map_create(union bpf_attr *attr, bool kernel)bpf_map_free (map); put_token:bpf_token_put (token); +if (err && log) + (void )bpf_vlog_finalize (log, &log_true_size); +if (log) +kvfree (log);return err; }
+1 to Andrii's suggestion to report log size back, just for consistency reasons.
Re: [RFC PATCH bpf-next v2 4/6] bpf: Add common attr support for map_create
Quoted 8 lines · Eduard Zingerman
On Fri, 2025-09-12 at 00:33 +0800, Leon Hwang wrote:
[...]
Quoted 4 lines
kernel/bpf/syscall.c +0
-0@@ -1355,6 +1356,18 @@ static int map_create(union bpf_attr *attr, bool kernel)if (err)return -EINVAL ;
Quoted 10 lines
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bool kernel) · line 1364
Quoted 6 lines
kernel/bpf/syscall.c +6
-0+if (common_attrs->log_buf) { + log =kvzalloc (sizeof (*log),GFP_KERNEL ); +if (!log) +return -ENOMEM ; + err =bpf_vlog_init (log,BPF_LOG_FIXED ,u64_to_user_ptr (common_attrs->log_buf), + common_attrs->log_size,NULL );Maybe use
common_attrs->log_levelinstead ofBPF_LOG_FIXED? Just for consistent behavior with program and btf load operations.
It doesn’t really make sense to let users set log_level in this case, because logging in map_create is too simple for different log levels to have any meaningful effect.
Quoted 12 lines
Quoted 9 lines
kernel/bpf/syscall.c +6
-0+if (err) { +kvfree (log); +return err; + } + } +/* check BPF_F_TOKEN_FD flag, remember if it's set, and then clear it * to avoid per-map type checks tripping on unknown flag */ [...]
Quoted 13 lines
kernel/bpf/syscall.c static int map_create(union bpf_attr *attr, bool kernel) · line 1613
Quoted 10 lines
kernel/bpf/syscall.c +4
-0@@ -1565,6 +1605,10 @@ static int map_create(union bpf_attr *attr, bool kernel)bpf_map_free (map); put_token:bpf_token_put (token); +if (err && log) + (void )bpf_vlog_finalize (log, &log_true_size); +if (log) +kvfree (log);return err; }+1 to Andrii's suggestion to report log size back, just for consistency reasons.
Right.
The log_true_size will be reported back to users.
Thanks,
Leon
RFC PATCH v2 5/6 libbpf: Add common attr support for map_create
In reply to Leon HwangWith the previous patch adding common attribute support for BPF_MAP_CREATE, it is now possible to retrieve detailed error messages when map creation fails by using the 'log_buf' field from the common attributes.
This patch extends 'bpf_map_create_opts' with two new fields, 'log_buf' and 'log_size', allowing users to capture and inspect these log messages.
--- tools/lib/bpf/bpf.c | 16 +++++++++++++++- tools/lib/bpf/bpf.h | 5 ++++- 2 files changed, 19 insertions(+), 2 deletions(-)
tools/lib/bpf/bpf.c +15 -1
@@ -218,7 +218,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, map_token_fd); +struct bpf_common_attr common_attrs;union bpf_attr attr; +__u64 log_buf;int fd;bump_rlimit_memlock ();
@@ -249,7 +251,19 @@ int bpf_map_create(enum bpf_map_type map_type, attr.map_token_fd =OPTS_GET (opts, token_fd,0 ); - fd =sys_bpf_fd (BPF_MAP_CREATE , &attr, attr_sz); + log_buf = (__u64 )OPTS_GET (opts, log_buf,NULL ); +if (log_buf) { +if (!feat_supported (NULL ,FEAT_EXTENDED_SYSCALL )) +return libbpf_err (-EOPNOTSUPP ); + +memset (&common_attrs,0 ,sizeof (common_attrs)); + common_attrs.log_buf = log_buf; + common_attrs.log_size =OPTS_GET (opts, log_size,0 ); + fd =sys_bpf_extended (BPF_MAP_CREATE , &attr, attr_sz, &common_attrs, +sizeof (common_attrs)); + }else { + fd =sys_bpf_fd (BPF_MAP_CREATE , &attr, attr_sz); + }return libbpf_err_errno (fd); }
tools/lib/bpf/bpf.h +4 -1
@@ -55,9 +55,12 @@ struct bpf_map_create_opts {__s32 value_type_btf_obj_fd;__u32 token_fd; + +const char *log_buf; +__u32 log_size; size_t :0; }; -#define bpf_map_create_opts__last_field token_fd +#define bpf_map_create_opts__last_field log_size LIBBPF_API int bpf_map_create(enum bpf_map_type map_type, const char *map_name,
Signature · 1 line
2.50.1
Re: [RFC PATCH bpf-next v2 5/6] libbpf: Add common attr support for map_create
Quoted 20 lines · Leon Hwang
With the previous patch adding common attribute support for
BPF_MAP_CREATE, it is now possible to retrieve detailed error messages when map creation fails by using the 'log_buf' field from the common attributes.This patch extends '
bpf_map_create_opts' with two new fields, 'log_buf' and 'log_size', allowing users to capture and inspect these log messages.Signed-off-by Leon Hwang <[email protected]>--- tools/lib/bpf/bpf.c | 16 +++++++++++++++- tools/lib/bpf/bpf.h | 5 ++++- 2 files changed, 19 insertions(+), 2 deletions(-)tools/lib/bpf/bpf.c +0
-0
tools/lib/bpf/bpf.c int bpf_map_create(enum bpf_map_type map_type, · line 223
Quoted 6 lines
tools/lib/bpf/bpf.c +2
-0@@ -218,7 +218,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, map_token_fd); +struct bpf_common_attr common_attrs;union bpf_attr attr; +__u64 log_buf;
const char *
tools/lib/bpf/bpf.c int bpf_map_create(enum bpf_map_type map_type, · line 254
Quoted 9 lines
tools/lib/bpf/bpf.c +1
-1int fd;bump_rlimit_memlock ();@@ -249,7 +251,19 @@ int bpf_map_create(enum bpf_map_type map_type, attr.map_token_fd =OPTS_GET (opts, token_fd,0 ); - fd =sys_bpf_fd (BPF_MAP_CREATE , &attr, attr_sz); + log_buf = (__u64 )OPTS_GET (opts, log_buf,NULL );
no u64 casting just yet
tools/lib/bpf/bpf.c int bpf_map_create(enum bpf_map_type map_type, · line 257
Quoted 3 lines
tools/lib/bpf/bpf.c +3
-0+if (log_buf) { +if (!feat_supported (NULL ,FEAT_EXTENDED_SYSCALL )) +return libbpf_err (-EOPNOTSUPP );
um.. I'm thinking that it would be better usability for libbpf to ignore provided log if kernel doesn't support this feature just yet. Then users don't have to care, they will just opportunistically provide buffer and get extra error log, if kernel supports this feature. Otherwise, log won't be touched, instead of failing an API call.
tools/lib/bpf/bpf.c int bpf_map_create(enum bpf_map_type map_type, · line 260
Quoted 3 lines
tools/lib/bpf/bpf.c +3
-0+ +memset (&common_attrs,0 ,sizeof (common_attrs)); + common_attrs.log_buf = log_buf;
ptr_to_u64(log_buf) here
Quoted 30 lines
tools/lib/bpf/bpf.c +6
-0+ common_attrs.log_size =OPTS_GET (opts, log_size,0 ); + fd =sys_bpf_extended (BPF_MAP_CREATE , &attr, attr_sz, &common_attrs, +sizeof (common_attrs)); + }else { + fd =sys_bpf_fd (BPF_MAP_CREATE , &attr, attr_sz); + }return libbpf_err_errno (fd); }tools/lib/bpf/bpf.h +4
-2@@ -55,9 +55,12 @@ struct bpf_map_create_opts {__s32 value_type_btf_obj_fd;__u32 token_fd; + +const char *log_buf; +__u32 log_size; size_t :0; }; -#define bpf_map_create_opts__last_field token_fd +#define bpf_map_create_opts__last_field log_size LIBBPF_API int bpf_map_create(enum bpf_map_type map_type, const char *map_name, --2.50.1
Re: [RFC PATCH bpf-next v2 5/6] libbpf: Add common attr support for map_create
Quoted 77 lines · Andrii Nakryiko
Quoted 20 lines · Leon Hwang
With the previous patch adding common attribute support for
BPF_MAP_CREATE, it is now possible to retrieve detailed error messages when map creation fails by using the 'log_buf' field from the common attributes.This patch extends '
bpf_map_create_opts' with two new fields, 'log_buf' and 'log_size', allowing users to capture and inspect these log messages.Signed-off-by Leon Hwang <[email protected]>--- tools/lib/bpf/bpf.c | 16 +++++++++++++++- tools/lib/bpf/bpf.h | 5 ++++- 2 files changed, 19 insertions(+), 2 deletions(-)tools/lib/bpf/bpf.c +0
-0tools/lib/bpf/bpf.c int bpf_map_create(enum bpf_map_type map_type, · line 223
Quoted 6 lines
tools/lib/bpf/bpf.c +2
-0@@ -218,7 +218,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, map_token_fd); +struct bpf_common_attr common_attrs;union bpf_attr attr; +__u64 log_buf;const char *
tools/lib/bpf/bpf.c int bpf_map_create(enum bpf_map_type map_type, · line 254
Quoted 9 lines
tools/lib/bpf/bpf.c +1
-1int fd;bump_rlimit_memlock ();@@ -249,7 +251,19 @@ int bpf_map_create(enum bpf_map_type map_type, attr.map_token_fd =OPTS_GET (opts, token_fd,0 ); - fd =sys_bpf_fd (BPF_MAP_CREATE , &attr, attr_sz); + log_buf = (__u64 )OPTS_GET (opts, log_buf,NULL );no u64 casting just yet
tools/lib/bpf/bpf.c int bpf_map_create(enum bpf_map_type map_type, · line 257
Quoted 3 lines
tools/lib/bpf/bpf.c +3
-0+if (log_buf) { +if (!feat_supported (NULL ,FEAT_EXTENDED_SYSCALL )) +return libbpf_err (-EOPNOTSUPP );um.. I'm thinking that it would be better usability for libbpf to ignore provided log if kernel doesn't support this feature just yet. Then users don't have to care, they will just opportunistically provide buffer and get extra error log, if kernel supports this feature. Otherwise, log won't be touched, instead of failing an API call.
tools/lib/bpf/bpf.c int bpf_map_create(enum bpf_map_type map_type, · line 260
Quoted 3 lines
tools/lib/bpf/bpf.c +3
-0+ +memset (&common_attrs,0 ,sizeof (common_attrs)); + common_attrs.log_buf = log_buf;
ptr_to_u64(log_buf)hereQuoted 16 lines
tools/lib/bpf/bpf.c +6
-0+ common_attrs.log_size =OPTS_GET (opts, log_size,0 ); + fd =sys_bpf_extended (BPF_MAP_CREATE , &attr, attr_sz, &common_attrs, +sizeof (common_attrs)); + }else { + fd =sys_bpf_fd (BPF_MAP_CREATE , &attr, attr_sz); + }return libbpf_err_errno (fd); }tools/lib/bpf/bpf.h +0
-0@@ -55,9 +55,12 @@ struct bpf_map_create_opts {__s32 value_type_btf_obj_fd;
Quoted 4 lines
tools/lib/bpf/bpf.h struct bpf_map_create_opts { · line 60
Quoted 4 lines
tools/lib/bpf/bpf.h +3
-0__u32 token_fd; + +const char *log_buf; +__u32 log_size;
also, what about that log_level ?
Quoted 10 lines
Quoted 10 lines
tools/lib/bpf/bpf.h +1
-2size_t :0; }; -#define bpf_map_create_opts__last_field token_fd +#define bpf_map_create_opts__last_field log_size LIBBPF_API int bpf_map_create(enum bpf_map_type map_type, const char *map_name, --2.50.1
Re: [RFC PATCH bpf-next v2 5/6] libbpf: Add common attr support for map_create
Quoted 19 lines · Andrii Nakryiko
Quoted 17 lines · Andrii Nakryiko
Quoted 15 lines · Leon Hwang
With the previous patch adding common attribute support for
BPF_MAP_CREATE, it is now possible to retrieve detailed error messages when map creation fails by using the 'log_buf' field from the common attributes.This patch extends '
bpf_map_create_opts' with two new fields, 'log_buf' and 'log_size', allowing users to capture and inspect these log messages.Signed-off-by Leon Hwang <[email protected]>--- tools/lib/bpf/bpf.c | 16 +++++++++++++++- tools/lib/bpf/bpf.h | 5 ++++- 2 files changed, 19 insertions(+), 2 deletions(-)
Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
tools/lib/bpf/bpf.c +0
-0@@ -218,7 +218,9 @@ int bpf_map_create(enum bpf_map_type map_type, const struct bpf_map_create_opts *opts)Quoted 9 lines
tools/lib/bpf/bpf.c int bpf_map_create(enum bpf_map_type map_type, · line 223
Quoted 5 lines
tools/lib/bpf/bpf.c +2
-0{ const size_t attr_sz = offsetofend(union bpf_attr, map_token_fd); +struct bpf_common_attr common_attrs;union bpf_attr attr; +__u64 log_buf;const char *
Ack.
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
tools/lib/bpf/bpf.c +0
-0int fd;bump_rlimit_memlock ();@@ -249,7 +251,19 @@ int bpf_map_create(enum bpf_map_type map_type,
Quoted 7 lines
Quoted 7 lines
tools/lib/bpf/bpf.c int bpf_map_create(enum bpf_map_type map_type, · line 254
Quoted 4 lines
tools/lib/bpf/bpf.c +1
-1attr.map_token_fd =OPTS_GET (opts, token_fd,0 ); - fd =sys_bpf_fd (BPF_MAP_CREATE , &attr, attr_sz); + log_buf = (__u64 )OPTS_GET (opts, log_buf,NULL );no u64 casting just yet
Ack.
Quoted 11 lines
Quoted 11 lines
tools/lib/bpf/bpf.c int bpf_map_create(enum bpf_map_type map_type, · line 257
Quoted 3 lines
tools/lib/bpf/bpf.c +3
-0+if (log_buf) { +if (!feat_supported (NULL ,FEAT_EXTENDED_SYSCALL )) +return libbpf_err (-EOPNOTSUPP );um.. I'm thinking that it would be better usability for libbpf to ignore provided log if kernel doesn't support this feature just yet. Then users don't have to care, they will just opportunistically provide buffer and get extra error log, if kernel supports this feature. Otherwise, log won't be touched, instead of failing an API call.
Agreed.
These two 'if's will be merged into one:
if (log_buf && feat_supported(NULL, FEAT_EXTENDED_SYSCALL)) {
...
}Quoted 6 lines
Quoted 6 lines
tools/lib/bpf/bpf.c int bpf_map_create(enum bpf_map_type map_type, · line 260
Quoted 3 lines
tools/lib/bpf/bpf.c +3
-0+ +memset (&common_attrs,0 ,sizeof (common_attrs)); + common_attrs.log_buf = log_buf;
ptr_to_u64(log_buf)here
Ack.
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
tools/lib/bpf/bpf.c +6
-0+ common_attrs.log_size =OPTS_GET (opts, log_size,0 ); + fd =sys_bpf_extended (BPF_MAP_CREATE , &attr, attr_sz, &common_attrs, +sizeof (common_attrs)); + }else { + fd =sys_bpf_fd (BPF_MAP_CREATE , &attr, attr_sz); + }return libbpf_err_errno (fd); }tools/lib/bpf/bpf.h +0
-0@@ -55,9 +55,12 @@ struct bpf_map_create_opts {__s32 value_type_btf_obj_fd;
Quoted 7 lines
Quoted 4 lines
tools/lib/bpf/bpf.h struct bpf_map_create_opts { · line 60
Quoted 4 lines
tools/lib/bpf/bpf.h +3
-0__u32 token_fd; + +const char *log_buf; +__u32 log_size;also, what about that
log_level?
Should we really introduce log_level here?
I don’t think it makes sense, because logging in map_create is too simple for different log levels on the kernel side to have any meaningful effect.
Thanks,
Leon
Re: [RFC PATCH bpf-next v2 5/6] libbpf: Add common attr support for map_create
Quoted 109 lines · Leon Hwang
Quoted 19 lines · Andrii Nakryiko
Quoted 17 lines · Andrii Nakryiko
Quoted 15 lines · Leon Hwang
With the previous patch adding common attribute support for
BPF_MAP_CREATE, it is now possible to retrieve detailed error messages when map creation fails by using the 'log_buf' field from the common attributes.This patch extends '
bpf_map_create_opts' with two new fields, 'log_buf' and 'log_size', allowing users to capture and inspect these log messages.Signed-off-by Leon Hwang <[email protected]>--- tools/lib/bpf/bpf.c | 16 +++++++++++++++- tools/lib/bpf/bpf.h | 5 ++++- 2 files changed, 19 insertions(+), 2 deletions(-)Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
tools/lib/bpf/bpf.c +0
-0@@ -218,7 +218,9 @@ int bpf_map_create(enum bpf_map_type map_type, const struct bpf_map_create_opts *opts)Quoted 9 lines
tools/lib/bpf/bpf.c int bpf_map_create(enum bpf_map_type map_type, · line 223
Quoted 5 lines
tools/lib/bpf/bpf.c +2
-0{ const size_t attr_sz = offsetofend(union bpf_attr, map_token_fd); +struct bpf_common_attr common_attrs;union bpf_attr attr; +__u64 log_buf;const char *
Ack.
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
tools/lib/bpf/bpf.c +0
-0int fd;bump_rlimit_memlock ();@@ -249,7 +251,19 @@ int bpf_map_create(enum bpf_map_type map_type,Quoted 7 lines
Quoted 7 lines
tools/lib/bpf/bpf.c int bpf_map_create(enum bpf_map_type map_type, · line 254
Quoted 4 lines
tools/lib/bpf/bpf.c +1
-1attr.map_token_fd =OPTS_GET (opts, token_fd,0 ); - fd =sys_bpf_fd (BPF_MAP_CREATE , &attr, attr_sz); + log_buf = (__u64 )OPTS_GET (opts, log_buf,NULL );no u64 casting just yet
Ack.
Quoted 11 lines
Quoted 11 lines
tools/lib/bpf/bpf.c int bpf_map_create(enum bpf_map_type map_type, · line 257
Quoted 3 lines
tools/lib/bpf/bpf.c +3
-0+if (log_buf) { +if (!feat_supported (NULL ,FEAT_EXTENDED_SYSCALL )) +return libbpf_err (-EOPNOTSUPP );um.. I'm thinking that it would be better usability for libbpf to ignore provided log if kernel doesn't support this feature just yet. Then users don't have to care, they will just opportunistically provide buffer and get extra error log, if kernel supports this feature. Otherwise, log won't be touched, instead of failing an API call.
Agreed.
These two 'if's will be merged into one:
if (log_buf && feat_supported(NULL, FEAT_EXTENDED_SYSCALL)) { ... }Quoted 6 lines
Quoted 6 lines
tools/lib/bpf/bpf.c int bpf_map_create(enum bpf_map_type map_type, · line 260
Quoted 3 lines
tools/lib/bpf/bpf.c +3
-0+ +memset (&common_attrs,0 ,sizeof (common_attrs)); + common_attrs.log_buf = log_buf;
ptr_to_u64(log_buf)hereAck.
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
tools/lib/bpf/bpf.c +6
-0+ common_attrs.log_size =OPTS_GET (opts, log_size,0 ); + fd =sys_bpf_extended (BPF_MAP_CREATE , &attr, attr_sz, &common_attrs, +sizeof (common_attrs)); + }else { + fd =sys_bpf_fd (BPF_MAP_CREATE , &attr, attr_sz); + }return libbpf_err_errno (fd); }tools/lib/bpf/bpf.h +0
-0@@ -55,9 +55,12 @@ struct bpf_map_create_opts {__s32 value_type_btf_obj_fd;Quoted 7 lines
Quoted 4 lines
tools/lib/bpf/bpf.h struct bpf_map_create_opts { · line 60
Quoted 4 lines
tools/lib/bpf/bpf.h +3
-0__u32 token_fd; + +const char *log_buf; +__u32 log_size;also, what about that
log_level?
Quoted 2 lines
Should we really introduce
log_levelhere?
yes, at the very least for API consistency. bpf() syscall's log is a) buffer, b) size of that bufer, c) log_level / flags. Let's keep it consistent.
Quoted 6 lines
I don’t think it makes sense, because logging in
map_createis too simple for different log levels on the kernel side to have any meaningful effect.Thanks,
Leon
RFC PATCH v2 6/6 selftests/bpf: Add cases to test map create failure log
In reply to Leon HwangAs kernel is able to report log when fail to create map, add test cases to check those logs.
cd tools/testing/selftests/bpf
./test_progs -t map_create_failure
191/1 map_create_failure/invalid_vmlinux_value_type_id:OK 191/2 map_create_failure/invalid_value_type_id:OK 191/3 map_create_failure/invalid_map_extra:OK 191/4 map_create_failure/invalid_numa_node:OK 191/5 map_create_failure/invalid_map_type:OK 191/6 map_create_failure/invalid_token_fd:OK 191/7 map_create_failure/invalid_map_name:OK 191/8 map_create_failure/invalid_btf_fd:OK 191 map_create_failure:OK
Summary: 1/8 PASSED, 0 SKIPPED, 0 FAILED
--- .../selftests/bpf/prog_tests/map_init.c | 124 ++++++++++++++++++ 1 file changed, 124 insertions(+)
tools/testing/selftests/bpf/prog_tests/map_init.c +124 -0
@@ -212,3 +212,127 @@ 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) +{ +char log_buf[128 ]; +int fd; + + log_buf[0 ] ='\0' ; + opts->log_buf = log_buf; + opts->log_size =sizeof (log_buf); + fd =bpf_map_create (map_type, map_name,4 ,4 ,1 , opts); +if (!ASSERT_LT (fd,0 ,"bpf_map_create" )) + goto out; + +ASSERT_STREQ (log_buf, exp_msg,"unexpected log_buf" ); +out: +if (fd >=0 ) +close (fd); +} + +static void test_map_create_simple (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 (void ) +{ +const char *msg ="Invalid use of btf_vmlinux_value_type_id.\n" ; +LIBBPF_OPTS (bpf_map_create_opts, opts, + .btf_vmlinux_value_type_id = 1, + ); + +test_map_create_simple (&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_simple (&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_simple (&opts, msg); +} + +static void test_invalid_numa_node (void ) +{ +const char *msg ="Invalid or offline numa_node.\n" ; +LIBBPF_OPTS (bpf_map_create_opts, opts, + .map_flags = BPF_F_NUMA_NODE, + .numa_node = 0xFF, + ); + +test_map_create_simple (&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_simple (&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_simple (&opts, msg); +} + +void test_map_create_failure (void ) +{ +if (test__start_subtest ("invalid_vmlinux_value_type_id" )) +test_invalid_vmlinux_value_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 (); +}
Signature · 1 line
2.50.1