From nobody Sat Feb 7 17:55:33 2026 Received: from out-186.mta0.migadu.com (out-186.mta0.migadu.com [91.218.175.186]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5DA0F34D4E4; Tue, 6 Jan 2026 17:20:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.186 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767720060; cv=none; b=CGIysP0UCSkKzZpLSiYl91yLLoIROQsJsZg91US64Di0wjYoePbNnokoCRQvp2nLUhdM1THCRUxZy1xaAmp5cTUtFZEVcj6txOXaqX51/p6cdfQwV0UWF5gFslKCwOG7g81/D3/FqomO8+LMQHbEPwYe2wsoO0WfKELol8tGK+Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767720060; c=relaxed/simple; bh=oxIVZtcibxIdlrQ7G9EZOx7tfCuyfYgUcq67L6JvSzw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qSYUwH6D7kKGe3Ls7NDA13HhX8QN8750rp+ERGoQkdzG9+He0iDhLi7YLcxKHBAnaExlE3TQCPDOZTRjRsS4Dha+mqGhR0TQwkv5RyUsH5/ChB4N9r3/M7b9ylVh6rVy0tlVzJrk1cXfsq9liDqNy0CD6q1OWSeUPY8mxg3mfjc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=JYC3BMro; arc=none smtp.client-ip=91.218.175.186 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="JYC3BMro" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1767720055; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ZUTAxF7YINRUTHUdOTMsQNv3ktVF4OtuYgRb7ZMltyA=; b=JYC3BMroxaBUPT3WVKVOx+ECGYzrd7VwPMrtmd2gljQKOTk8rLjQilWzK1Zm7viLE5Id7f gi9Mh5SO8TLkVmXDLF+Fg5xzeSzIkQ1OzQjptjEPOjotjyafKdX/Xtk3zVqHY4AUQdGyHi bfOW3E4b8T2IZOaRigUxPDBEb1keE10= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , Christian Brauner , Oleg Nesterov , Leon Hwang , Seth Forshee , Yuichiro Tsuji , Andrey Albershteyn , Willem de Bruijn , Jason Xing , Paul Chaignon , Mykyta Yatsenko , Kumar Kartikeya Dwivedi , Anton Protopopov , Amery Hung , Rong Tao , linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, linux-kselftest@vger.kernel.org, kernel-patches-bot@fb.com Subject: [RESEND PATCH bpf-next v4 1/9] bpf: Extend bpf syscall with common attributes support Date: Wed, 7 Jan 2026 01:20:10 +0800 Message-ID: <20260106172018.57757-2-leon.hwang@linux.dev> In-Reply-To: <20260106172018.57757-1-leon.hwang@linux.dev> References: <20260106172018.57757-1-leon.hwang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Extend the BPF syscall to support a set of common attributes shared across all BPF commands: 1. 'log_buf': User-provided buffer for storing logs. 2. 'log_size': Size of the log buffer. 3. 'log_level': Log verbosity level. 4. 'log_true_size': The size of log reported by kernel. These common attributes are passed as the 4th argument to the BPF syscall, with the 5th argument specifying the size of this structure. To indicate the use of these common attributes from userspace, a new flag 'BPF_COMMON_ATTRS' ('1 << 16') is introduced. This flag is OR-ed into the 'cmd' field of the syscall. When 'cmd & BPF_COMMON_ATTRS' is set, the kernel will copy the common attributes from userspace into kernel space for use. Signed-off-by: Leon Hwang --- 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(-) diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h index cf84d98964b2..729659202d77 100644 --- a/include/linux/syscalls.h +++ b/include/linux/syscalls.h @@ -937,7 +937,8 @@ asmlinkage long sys_seccomp(unsigned int op, unsigned i= nt 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 in= t 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); diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 84ced3ed2d21..dcae1f3e50b7 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -986,6 +986,7 @@ enum bpf_cmd { BPF_PROG_STREAM_READ_BY_FD, BPF_PROG_ASSOC_STRUCT_OPS, __MAX_BPF_CMD, + BPF_COMMON_ATTRS =3D 1 << 16, /* Indicate carrying bpf_common_attr. */ }; =20 enum bpf_map_type { @@ -1489,6 +1490,13 @@ struct bpf_stack_build_id { }; }; =20 +struct bpf_common_attr { + __u64 log_buf; + __u32 log_size; + __u32 log_level; + __u32 log_true_size; +}; + #define BPF_OBJ_NAME_LEN 16U =20 enum { diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 6dd2ad2f9e81..8f464b847405 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -6160,8 +6160,10 @@ static int prog_assoc_struct_ops(union bpf_attr *att= r) return ret; } =20 -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; =20 @@ -6175,6 +6177,20 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uatt= r, unsigned int size) if (copy_from_bpfptr(&attr, uattr, size) !=3D 0) return -EFAULT; =20 + memset(&common_attrs, 0, sizeof(common_attrs)); + if (cmd & BPF_COMMON_ATTRS) { + err =3D bpf_check_uarg_tail_zero(uattr_common, sizeof(common_attrs), siz= e_common); + if (err) + return err; + + cmd &=3D ~BPF_COMMON_ATTRS; + size_common =3D min_t(u32, size_common, sizeof(common_attrs)); + if (copy_from_bpfptr(&common_attrs, uattr_common, size_common) !=3D 0) + return -EFAULT; + } else { + size_common =3D 0; + } + err =3D security_bpf(cmd, &attr, size, uattr.is_kernel); if (err < 0) return err; @@ -6310,9 +6326,10 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uatt= r, unsigned int size) return err; } =20 -SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned in= t, size) +SYSCALL_DEFINE5(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned in= t, 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); } =20 static bool syscall_prog_is_valid_access(int off, int size, @@ -6343,7 +6360,7 @@ BPF_CALL_3(bpf_sys_bpf, int, cmd, union bpf_attr *, a= ttr, 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); } =20 =20 diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 6b92b0847ec2..2cb847b38f20 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -986,6 +986,7 @@ enum bpf_cmd { BPF_PROG_STREAM_READ_BY_FD, BPF_PROG_ASSOC_STRUCT_OPS, __MAX_BPF_CMD, + BPF_COMMON_ATTRS =3D 1 << 16, /* Indicate carrying bpf_common_attr. */ }; =20 enum bpf_map_type { @@ -1489,6 +1490,13 @@ struct bpf_stack_build_id { }; }; =20 +struct bpf_common_attr { + __u64 log_buf; + __u32 log_size; + __u32 log_level; + __u32 log_true_size; +}; + #define BPF_OBJ_NAME_LEN 16U =20 enum { --=20 2.52.0 From nobody Sat Feb 7 17:55:33 2026 Received: from out-186.mta0.migadu.com (out-186.mta0.migadu.com [91.218.175.186]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2E5BE34DB5B for ; Tue, 6 Jan 2026 17:21:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.186 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767720069; cv=none; b=g7fK9I/nUuLm7f+a0a7gijlWaJM8vt1R+0fGJlMtgIZC/s2wDnm4VtRMKthixJBGbI4vyLa8lnwCIOwNiN60ZyfEHTYIG+90ZKJynejE8DTVVh4DrBCIZ8KhoYx0rq+sXC5QEXLAwEuUCLAzdkqM6V10e0Iy9f+K7ZO07wZ14mA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767720069; c=relaxed/simple; bh=qdZdan8Bg/3zMmLeA4fE/+22k8U9TKV55zGuSzKMjWA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QhrOQCFwUe/txgiqPbE8gz4ObHjD6AW04KAy09WYXEHJeadIN52EXuEfikjrqyJViXduaBEPXpiJB4zJmZnaM9LVsFs17NrA8i7AVNi3tavAzLLc7tgjpnuS38n8Gf/qRl2tdaBgzb7Yzl2Pkh0qYZp6DELwbfVGsWv0/yuFFpI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=t9Ap5Gi9; arc=none smtp.client-ip=91.218.175.186 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="t9Ap5Gi9" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1767720065; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=el+zFEiv5MlCth3d6SGPMrwMvtewF+0XV0F7CsdC9cM=; b=t9Ap5Gi9xp+auW0Bk3FclI8wYWxDTul6sjO24BmGqXJzJVxJuhAIddY9hDLV6YoNVJWu1Q i1Ee+p2HseJFBRIA5e0LT2JLW4uuGPTyIgYxs4/w7aieStTDrnsyRj6/5k/BNinD1tpM5R PvTvU9SZ+kYBOiPddh9bLE5Ap7IHoMI= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , Christian Brauner , Oleg Nesterov , Leon Hwang , Seth Forshee , Yuichiro Tsuji , Andrey Albershteyn , Willem de Bruijn , Jason Xing , Paul Chaignon , Mykyta Yatsenko , Kumar Kartikeya Dwivedi , Anton Protopopov , Amery Hung , Rong Tao , linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, linux-kselftest@vger.kernel.org, kernel-patches-bot@fb.com Subject: [RESEND PATCH bpf-next v4 2/9] libbpf: Add support for extended bpf syscall Date: Wed, 7 Jan 2026 01:20:11 +0800 Message-ID: <20260106172018.57757-3-leon.hwang@linux.dev> In-Reply-To: <20260106172018.57757-1-leon.hwang@linux.dev> References: <20260106172018.57757-1-leon.hwang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" To support the extended BPF syscall introduced in the previous commit, introduce the following internal APIs: * 'sys_bpf_ext()' * 'sys_bpf_ext_fd()' They wrap the raw 'syscall()' interface to support passing extended attributes. * 'probe_sys_bpf_ext()' Check whether current kernel supports the extended attributes. Signed-off-by: Leon Hwang --- tools/lib/bpf/bpf.c | 34 +++++++++++++++++++++++++++++++++ tools/lib/bpf/features.c | 8 ++++++++ tools/lib/bpf/libbpf_internal.h | 3 +++ 3 files changed, 45 insertions(+) diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index 21b57a629916..689ade4a822b 100644 --- a/tools/lib/bpf/bpf.c +++ b/tools/lib/bpf/bpf.c @@ -69,6 +69,40 @@ static inline __u64 ptr_to_u64(const void *ptr) return (__u64) (unsigned long) ptr; } =20 +static inline int sys_bpf_ext(enum bpf_cmd cmd, union bpf_attr *attr, + unsigned int size, + struct bpf_common_attr *common_attrs, + unsigned int size_common) +{ + cmd =3D common_attrs ? (cmd | BPF_COMMON_ATTRS) : (cmd & ~BPF_COMMON_ATTR= S); + return syscall(__NR_bpf, cmd, attr, size, common_attrs, size_common); +} + +static inline int sys_bpf_ext_fd(enum bpf_cmd cmd, union bpf_attr *attr, + unsigned int size, + struct bpf_common_attr *common_attrs, + unsigned int size_common) +{ + int fd; + + fd =3D sys_bpf_ext(cmd, attr, size, common_attrs, size_common); + return ensure_good_fd(fd); +} + +int probe_sys_bpf_ext(void) +{ + const size_t attr_sz =3D offsetofend(union bpf_attr, prog_token_fd); + union bpf_attr attr; + int fd; + + memset(&attr, 0, attr_sz); + fd =3D syscall(__NR_bpf, BPF_PROG_LOAD | BPF_COMMON_ATTRS, &attr, attr_sz= , NULL, + sizeof(struct bpf_common_attr)); + if (fd >=3D 0) + close(fd); + return errno =3D=3D EFAULT; +} + static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr, unsigned int size) { diff --git a/tools/lib/bpf/features.c b/tools/lib/bpf/features.c index b842b83e2480..d786a815f1ae 100644 --- a/tools/lib/bpf/features.c +++ b/tools/lib/bpf/features.c @@ -506,6 +506,11 @@ static int probe_kern_arg_ctx_tag(int token_fd) return probe_fd(prog_fd); } =20 +static int probe_kern_extended_syscall(int token_fd) +{ + return probe_sys_bpf_ext(); +} + typedef int (*feature_probe_fn)(int /* token_fd */); =20 static struct kern_feature_cache feature_cache; @@ -581,6 +586,9 @@ static struct kern_feature_desc { [FEAT_BTF_QMARK_DATASEC] =3D { "BTF DATASEC names starting from '?'", probe_kern_btf_qmark_datasec, }, + [FEAT_EXTENDED_SYSCALL] =3D { + "Kernel supports extended syscall", probe_kern_extended_syscall, + }, }; =20 bool feat_supported(struct kern_feature_cache *cache, enum kern_feature_id= feat_id) diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_interna= l.h index fc59b21b51b5..e2a6ef4b45ae 100644 --- a/tools/lib/bpf/libbpf_internal.h +++ b/tools/lib/bpf/libbpf_internal.h @@ -392,6 +392,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, }; =20 @@ -757,4 +759,5 @@ int probe_fd(int fd); #define SHA256_DWORD_SIZE SHA256_DIGEST_LENGTH / sizeof(__u64) =20 void libbpf_sha256(const void *data, size_t len, __u8 out[SHA256_DIGEST_LE= NGTH]); +int probe_sys_bpf_ext(void); #endif /* __LIBBPF_LIBBPF_INTERNAL_H */ --=20 2.52.0 From nobody Sat Feb 7 17:55:33 2026 Received: from out-177.mta0.migadu.com (out-177.mta0.migadu.com [91.218.175.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2454934E745; Tue, 6 Jan 2026 17:21:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767720079; cv=none; b=kpNUkMa0cw6scyGAEiVT4Q/3JshxALT63Pwwbk/RjdSaA5alKLUr2kammA84MdzneBb0WQzhr8HW5G7tuTXkd9sdURZnWt/vFK5rGbhn/pXWw0WbkRE8EQMUNs9v6QFoAv9vajzmNZwlCC5tGIYfOQYyYjIjPv7cwl+JQbJuJVU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767720079; c=relaxed/simple; bh=2xF3Qg7R7k/WI3hP93EJEWfS637X1esGtX52brV56KU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=D4rVA4ldzqbTeKu+/CGAq43APLHfrc20zwQLBWpfwIUHoJe0S9OaiysGbZ78+fiW9iM4BDxI9PymQXTj8f8FnAXrLJ0O/lnIpBq5ilzitCQKDRgY+xcsNc9GKTa04KxIc56ZBcry/AeWi8h3rvv8CiiuhPpL3Fj8aXbKcdxkkhA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=m/zo5dFR; arc=none smtp.client-ip=91.218.175.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="m/zo5dFR" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1767720075; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ysQ2azkxtQw0iBk6HAA7r3CcBjC7e+Y/6e3WSQkcWpk=; b=m/zo5dFRxqIm5r2AFE/gYklYwny46MC3ZvBuJTzSXABwL+jBBW9F+95l4fA2xiM7S6gC7i rriJBGWdGlcbB1aIKdW/7xjZJOvOYlVSmdz2t0wxPXEEftiPtBsw849wzG2wK/cXdhglFa NDc5HhuOpIRe9snrSg44YfqiDpWOHZA= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , Christian Brauner , Oleg Nesterov , Leon Hwang , Seth Forshee , Yuichiro Tsuji , Andrey Albershteyn , Willem de Bruijn , Jason Xing , Paul Chaignon , Mykyta Yatsenko , Kumar Kartikeya Dwivedi , Anton Protopopov , Amery Hung , Rong Tao , linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, linux-kselftest@vger.kernel.org, kernel-patches-bot@fb.com Subject: [RESEND PATCH bpf-next v4 3/9] bpf: Refactor reporting log_true_size for prog_load Date: Wed, 7 Jan 2026 01:20:12 +0800 Message-ID: <20260106172018.57757-4-leon.hwang@linux.dev> In-Reply-To: <20260106172018.57757-1-leon.hwang@linux.dev> References: <20260106172018.57757-1-leon.hwang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" In the next commit, it will be able to report logs via extended common attributes, which will report 'log_true_size' via the extended common attributes meanwhile. Therefore, refactor the way of 'log_true_size' reporting in order to report 'log_true_size' via the extended common attributes easily. Signed-off-by: Leon Hwang --- include/linux/bpf.h | 2 +- kernel/bpf/syscall.c | 21 +++++++++++++++++---- kernel/bpf/verifier.c | 12 ++---------- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index a63e47d2109c..26fbc550e5aa 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -2868,7 +2868,7 @@ int bpf_check_uarg_tail_zero(bpfptr_t uaddr, size_t e= xpected_size, size_t actual_size); =20 /* 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); =20 #ifndef CONFIG_BPF_JIT_ALWAYS_ON void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth); diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 8f464b847405..1739601fb7bd 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -2862,7 +2862,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 =20 -static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_s= ize) +static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr) { enum bpf_prog_type type =3D attr->prog_type; struct bpf_prog *prog, *dst_prog =3D NULL; @@ -3080,7 +3080,7 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr= _t uattr, u32 uattr_size) goto free_prog_sec; =20 /* run eBPF verifier */ - err =3D bpf_check(&prog, attr, uattr, uattr_size); + err =3D bpf_check(&prog, attr, uattr); if (err < 0) goto free_used_maps; =20 @@ -6160,12 +6160,22 @@ static int prog_assoc_struct_ops(union bpf_attr *at= tr) return ret; } =20 +static int copy_prog_load_log_true_size(union bpf_attr *attr, bpfptr_t uat= tr, unsigned int size) +{ + if (size >=3D offsetofend(union bpf_attr, log_true_size) && + copy_to_bpfptr_offset(uattr, offsetof(union bpf_attr, log_true_size), + &attr->log_true_size, sizeof(attr->log_true_size))) + return -EFAULT; + + return 0; +} + 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; + int err, ret; =20 err =3D bpf_check_uarg_tail_zero(uattr, sizeof(attr), size); if (err) @@ -6215,7 +6225,10 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uatt= r, unsigned int size, err =3D map_freeze(&attr); break; case BPF_PROG_LOAD: - err =3D bpf_prog_load(&attr, uattr, size); + attr.log_true_size =3D 0; + err =3D bpf_prog_load(&attr, uattr); + ret =3D copy_prog_load_log_true_size(&attr, uattr, size); + err =3D ret ? ret : err; break; case BPF_OBJ_PIN: err =3D bpf_obj_pin(&attr); diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 9394b0de2ef0..ab5eacdde92c 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -25096,12 +25096,11 @@ static int compute_scc(struct bpf_verifier_env *e= nv) return err; } =20 -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) { u64 start_time =3D ktime_get_ns(); struct bpf_verifier_env *env; int i, len, ret =3D -EINVAL, err; - u32 log_true_size; bool is_priv; =20 BTF_TYPE_EMIT(enum bpf_features); @@ -25300,17 +25299,10 @@ int bpf_check(struct bpf_prog **prog, union bpf_a= ttr *attr, bpfptr_t uattr, __u3 env->prog->aux->verified_insns =3D env->insn_processed; =20 /* preserve original error even if log finalization is successful */ - err =3D bpf_vlog_finalize(&env->log, &log_true_size); + err =3D bpf_vlog_finalize(&env->log, &attr->log_true_size); if (err) ret =3D err; =20 - if (uattr_size >=3D 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 =3D -EFAULT; - goto err_release_maps; - } - if (ret) goto err_release_maps; =20 --=20 2.52.0 From nobody Sat Feb 7 17:55:33 2026 Received: from out-186.mta0.migadu.com (out-186.mta0.migadu.com [91.218.175.186]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 05BD334EF0A; Tue, 6 Jan 2026 17:21:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.186 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767720088; cv=none; b=QSnCO0hO6dLsbmu56wfRWuJXu7CxNROeetjrJ0ZhiwVJH1tjOb6vsDReneTjnv9zDFaoPFtZboVLkzEKxDjLo1FuHlxWQPVXJlLpLsJxpUp4po5whc8Nsxx9VoFnmqOBH9uxfoMaAkxZtua91zF18wlSbC/oQhUU1aUb4eh1hgI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767720088; c=relaxed/simple; bh=hc5NWGN0r9aDg0436s6gyQhJpYTeqZRW/K8Um66wE6I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=AuL0LwRVqkI2AwDcGHYTN5gfAvBITTlO6xHDHe/jEovkG248KPuO5ovH3go208GW1U9pPjtkzwY7a76Dh7zs7AXUvD9MDjEsu9m/rjJLxxSBE1ktXdW3Suf4X057PXjBTUM2SyjWyRSJ1FvtjnWpUh5x+XnGAsCmqhgemYRoTrQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=N0nWB/2u; arc=none smtp.client-ip=91.218.175.186 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="N0nWB/2u" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1767720083; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iRP7dSPyiBjJ0689O8KZb3Eqq91g9ONGcIKtAwQLB2U=; b=N0nWB/2u94N4Fx/mQWxeBSMJm9HIK1iuKVEcpgjPCGkOsQUgPvg10dzX3fiRo1lVTxKE4X MnYc5YFyEdnTUVBJOXCn7NlrGOiVk/RRZkIg0lS3gopoFdHZEjTA/PSzPpNRmkRSyXazFf pkYWARmxfVOED6vMhxP8t4FOSS65Zwc= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , Christian Brauner , Oleg Nesterov , Leon Hwang , Seth Forshee , Yuichiro Tsuji , Andrey Albershteyn , Willem de Bruijn , Jason Xing , Paul Chaignon , Mykyta Yatsenko , Kumar Kartikeya Dwivedi , Anton Protopopov , Amery Hung , Rong Tao , linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, linux-kselftest@vger.kernel.org, kernel-patches-bot@fb.com Subject: [RESEND PATCH bpf-next v4 4/9] bpf: Add common attr support for prog_load Date: Wed, 7 Jan 2026 01:20:13 +0800 Message-ID: <20260106172018.57757-5-leon.hwang@linux.dev> In-Reply-To: <20260106172018.57757-1-leon.hwang@linux.dev> References: <20260106172018.57757-1-leon.hwang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" The log buffer of common attributes would be confusing with the one in 'union bpf_attr' for BPF_PROG_LOAD. In order to clarify the usage of these two log buffers, 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 totally, return -EUSERS. Signed-off-by: Leon Hwang --- kernel/bpf/syscall.c | 51 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 1739601fb7bd..ad565f569a4f 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -6160,14 +6160,55 @@ static int prog_assoc_struct_ops(union bpf_attr *at= tr) return ret; } =20 -static int copy_prog_load_log_true_size(union bpf_attr *attr, bpfptr_t uat= tr, unsigned int size) +static int check_log_attrs(u64 log_buf, u32 log_size, u32 log_level, + struct bpf_common_attr *common_attrs) +{ + if (log_buf && common_attrs->log_buf && (log_buf !=3D common_attrs->log_b= uf || + log_size !=3D common_attrs->log_size || + log_level !=3D common_attrs->log_level)) + return -EUSERS; + + return 0; +} + +static int check_prog_load_log_attrs(union bpf_attr *attr, struct bpf_comm= on_attr *common_attrs) +{ + int err; + + err =3D check_log_attrs(attr->log_buf, attr->log_size, attr->log_level, c= ommon_attrs); + if (err) + return err; + + if (!attr->log_buf && common_attrs->log_buf) { + attr->log_buf =3D common_attrs->log_buf; + attr->log_size =3D common_attrs->log_size; + attr->log_level =3D common_attrs->log_level; + } + + return 0; +} + +static int copy_common_attr_log_true_size(bpfptr_t uattr, unsigned int siz= e, u32 *log_true_size) +{ + if (size >=3D offsetofend(struct bpf_common_attr, log_true_size) && + copy_to_bpfptr_offset(uattr, offsetof(struct bpf_common_attr, log_tru= e_size), + log_true_size, sizeof(*log_true_size))) + return -EFAULT; + + return 0; +} + +static int copy_prog_load_log_true_size(union bpf_attr *attr, bpfptr_t uat= tr, unsigned int size, + struct bpf_common_attr *common_attrs, bpfptr_t uattr_common, + unsigned int size_common) { if (size >=3D offsetofend(union bpf_attr, log_true_size) && copy_to_bpfptr_offset(uattr, offsetof(union bpf_attr, log_true_size), &attr->log_true_size, sizeof(attr->log_true_size))) return -EFAULT; =20 - return 0; + return copy_common_attr_log_true_size(uattr_common, size_common, + &attr->log_true_size); } =20 static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size, @@ -6225,9 +6266,13 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uatt= r, unsigned int size, err =3D map_freeze(&attr); break; case BPF_PROG_LOAD: + err =3D check_prog_load_log_attrs(&attr, &common_attrs); + if (err) + break; attr.log_true_size =3D 0; err =3D bpf_prog_load(&attr, uattr); - ret =3D copy_prog_load_log_true_size(&attr, uattr, size); + ret =3D copy_prog_load_log_true_size(&attr, uattr, size, &common_attrs, = uattr_common, + size_common); err =3D ret ? ret : err; break; case BPF_OBJ_PIN: --=20 2.52.0 From nobody Sat Feb 7 17:55:33 2026 Received: from out-171.mta0.migadu.com (out-171.mta0.migadu.com [91.218.175.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 02B0233C1B4 for ; Tue, 6 Jan 2026 17:21:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767720097; cv=none; b=BLYP4fGTL32y1qmQ1sZ91fIXPVMs7ecbMtxF2ubWokocfVn0ISFg+IPBxC38SMBvfQ8QQBxlmGl4FYbLUr0HOW1IZd8ThA8rzX0lEOAXmBI9Ipi1t9nsSW3LLMtKWK3yR4q6VU6eglx99hWuMgXfRP4HY+ZRfBJ3DhdYjl19Xts= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767720097; c=relaxed/simple; bh=Ftj/YzLelibUqHaEwRcMBZFhoEUuV8xe/0iLYvtKu9w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=khYQ5flodVxqSTJbFLVldXJQdc6yyt/LI6Bx70KZHNajEkCzvyYptgb4xKz7rJ1edxDTpXs4gbtH8useh0w58i+8MKrA6/xfjC8vILnsg+4bnGwcv9kUklwRJhQsgQYtC+ZCRSVSV3/YGxaxQih8KAKFChFQlWv4oNoIxxvf5vk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=WMzeYfob; arc=none smtp.client-ip=91.218.175.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="WMzeYfob" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1767720094; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EC0zW95wgB8/pMIwQU9ZWEcVV2VXSlvtnuFHgDLceqM=; b=WMzeYfobucZ1/+Urcx50cT64F4v6LsTpK+40dujFuPGlwA2unCvtZ/TdjdeN1UghnVgBl4 N6CgttcAAkAG0XsQ7nnLM9vyUombNMFFkmi6CKJUlH45N8hIT5iaOZS8OUPDlOXpEk3Dnb FQJ2dsZ879BQZY+vewgkO/fC7/IsDPk= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , Christian Brauner , Oleg Nesterov , Leon Hwang , Seth Forshee , Yuichiro Tsuji , Andrey Albershteyn , Willem de Bruijn , Jason Xing , Paul Chaignon , Mykyta Yatsenko , Kumar Kartikeya Dwivedi , Anton Protopopov , Amery Hung , Rong Tao , linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, linux-kselftest@vger.kernel.org, kernel-patches-bot@fb.com Subject: [RESEND PATCH bpf-next v4 5/9] bpf: Refactor reporting btf_log_true_size for btf_load Date: Wed, 7 Jan 2026 01:20:14 +0800 Message-ID: <20260106172018.57757-6-leon.hwang@linux.dev> In-Reply-To: <20260106172018.57757-1-leon.hwang@linux.dev> References: <20260106172018.57757-1-leon.hwang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" In the next commit, it will be able to report logs via extended common attributes, which will report 'log_true_size' via the extended common attributes meanwhile. Therefore, refactor the way of 'btf_log_true_size' reporting in order to report 'log_true_size' via the extended common attributes easily. Signed-off-by: Leon Hwang --- include/linux/btf.h | 2 +- kernel/bpf/btf.c | 25 +++++-------------------- kernel/bpf/syscall.c | 19 ++++++++++++++++--- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/include/linux/btf.h b/include/linux/btf.h index 691f09784933..2b27fdd567f5 100644 --- a/include/linux/btf.h +++ b/include/linux/btf.h @@ -144,7 +144,7 @@ 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(union bpf_attr *attr, bpfptr_t uattr); struct btf *btf_get_by_fd(int fd); int btf_get_info_by_fd(const struct btf *btf, const union bpf_attr *attr, diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 539c9fdea41d..9efcbb489edb 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -5745,22 +5745,7 @@ static int btf_check_type_tags(struct btf_verifier_e= nv *env, return 0; } =20 -static int finalize_log(struct bpf_verifier_log *log, bpfptr_t uattr, u32 = uattr_size) -{ - u32 log_true_size; - int err; - - err =3D bpf_vlog_finalize(log, &log_true_size); - - if (uattr_size >=3D offsetofend(union bpf_attr, btf_log_true_size) && - copy_to_bpfptr_offset(uattr, offsetof(union bpf_attr, btf_log_true_si= ze), - &log_true_size, sizeof(log_true_size))) - err =3D -EFAULT; - - return err; -} - -static struct btf *btf_parse(const union bpf_attr *attr, bpfptr_t uattr, u= 32 uattr_size) +static struct btf *btf_parse(union bpf_attr *attr, bpfptr_t uattr) { bpfptr_t btf_data =3D make_bpfptr(attr->btf, uattr.is_kernel); char __user *log_ubuf =3D u64_to_user_ptr(attr->btf_log_buf); @@ -5841,7 +5826,7 @@ static struct btf *btf_parse(const union bpf_attr *at= tr, bpfptr_t uattr, u32 uat } } =20 - err =3D finalize_log(&env->log, uattr, uattr_size); + err =3D bpf_vlog_finalize(&env->log, &attr->btf_log_true_size); if (err) goto errout_free; =20 @@ -5853,7 +5838,7 @@ static struct btf *btf_parse(const union bpf_attr *at= tr, bpfptr_t uattr, u32 uat btf_free_struct_meta_tab(btf); errout: /* overwrite err with -ENOSPC or -EFAULT */ - ret =3D finalize_log(&env->log, uattr, uattr_size); + ret =3D bpf_vlog_finalize(&env->log, &attr->btf_log_true_size); if (ret) err =3D ret; errout_free: @@ -8017,12 +8002,12 @@ static int __btf_new_fd(struct btf *btf) return anon_inode_getfd("btf", &btf_fops, btf, O_RDONLY | O_CLOEXEC); } =20 -int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size) +int btf_new_fd(union bpf_attr *attr, bpfptr_t uattr) { struct btf *btf; int ret; =20 - btf =3D btf_parse(attr, uattr, uattr_size); + btf =3D btf_parse(attr, uattr); if (IS_ERR(btf)) return PTR_ERR(btf); =20 diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index ad565f569a4f..ce349a059d5d 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -5422,7 +5422,7 @@ static int bpf_obj_get_info_by_fd(const union bpf_att= r *attr, =20 #define BPF_BTF_LOAD_LAST_FIELD btf_token_fd =20 -static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr, __u32 = uattr_size) +static int bpf_btf_load(union bpf_attr *attr, bpfptr_t uattr) { struct bpf_token *token =3D NULL; =20 @@ -5449,7 +5449,7 @@ static int bpf_btf_load(const union bpf_attr *attr, b= pfptr_t uattr, __u32 uattr_ =20 bpf_token_put(token); =20 - return btf_new_fd(attr, uattr, uattr_size); + return btf_new_fd(attr, uattr); } =20 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD fd_by_id_token_fd @@ -6211,6 +6211,16 @@ static int copy_prog_load_log_true_size(union bpf_at= tr *attr, bpfptr_t uattr, un &attr->log_true_size); } =20 +static int copy_btf_load_log_true_size(union bpf_attr *attr, bpfptr_t uatt= r, unsigned int size) +{ + if (size >=3D offsetofend(union bpf_attr, btf_log_true_size) && + copy_to_bpfptr_offset(uattr, offsetof(union bpf_attr, btf_log_true_si= ze), + &attr->btf_log_true_size, sizeof(attr->btf_log_true_size))) + return -EFAULT; + + return 0; +} + static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size, bpfptr_t uattr_common, unsigned int size_common) { @@ -6318,7 +6328,10 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uatt= r, unsigned int size, err =3D bpf_raw_tracepoint_open(&attr); break; case BPF_BTF_LOAD: - err =3D bpf_btf_load(&attr, uattr, size); + attr.btf_log_true_size =3D 0; + err =3D bpf_btf_load(&attr, uattr); + ret =3D copy_btf_load_log_true_size(&attr, uattr, size); + err =3D ret ? ret : err; break; case BPF_BTF_GET_FD_BY_ID: err =3D bpf_btf_get_fd_by_id(&attr); --=20 2.52.0 From nobody Sat Feb 7 17:55:33 2026 Received: from out-185.mta0.migadu.com (out-185.mta0.migadu.com [91.218.175.185]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3ADE2346A18 for ; Tue, 6 Jan 2026 17:21:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.185 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767720107; cv=none; b=iYyxoh57C7kNFVpQEDtxdA/rzlbr9ky0kZy3hvr9R3WDOOCz8O1ah14oi/KN75YGwUHgZFG9whNIWrRo8ntzGX2BGq4xxb26OvYiu7iebhC6aEI3l5GkqMRY1kvZMiqyq3NMSawETUBsroqi8GrXVPommKD6fwNATvStQQ8zAWY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767720107; c=relaxed/simple; bh=574nViD3Ekg7B8qaFiH5i9ESrWlBcUwH8a7+I+wJd0k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Rad7ez8hmKuEG7C3bRZPfzOlWTW97gL14r0BpZACeiHznebeWG8rDxbfP6DWQ7soF1dj6zh9/VbVOK0mpva108Ped3vd4mzcoTiJJzJHq7UHTPlR9h7yF4BRpt34nr36VGEPspfMUoJgKpPjgzqAP5BdE5fQ1jwvh6paEHz3XUw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=sP3loB8b; arc=none smtp.client-ip=91.218.175.185 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="sP3loB8b" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1767720103; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=oH5uvc/HIHJHvUDH1FHA2g822OEsdqyIpxTdGkj+aPM=; b=sP3loB8byZBmR1LqrU23UR+FYC4Bf9UVEJ+RmM5goMOD5zk9VH84b1iMRuqdkqv6FjgwJ3 jTH71FXVn2/x1ceZcPUq6eVUtzkkpv0BJb2h1B4NehHtfqn8EMhft/BNSTFp8RqmIV/Gc+ yKyC8WxozOJGoCDx01HYSm+/dm4CfH0= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , Christian Brauner , Oleg Nesterov , Leon Hwang , Seth Forshee , Yuichiro Tsuji , Andrey Albershteyn , Willem de Bruijn , Jason Xing , Paul Chaignon , Mykyta Yatsenko , Kumar Kartikeya Dwivedi , Anton Protopopov , Amery Hung , Rong Tao , linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, linux-kselftest@vger.kernel.org, kernel-patches-bot@fb.com Subject: [RESEND PATCH bpf-next v4 6/9] bpf: Add common attr support for btf_load Date: Wed, 7 Jan 2026 01:20:15 +0800 Message-ID: <20260106172018.57757-7-leon.hwang@linux.dev> In-Reply-To: <20260106172018.57757-1-leon.hwang@linux.dev> References: <20260106172018.57757-1-leon.hwang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" The log buffer of common attributes would be confusing with the one in 'union bpf_attr' for BPF_BTF_LOAD. In order to clarify the usage of these two log buffers, 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 totally, return -EUSERS. Signed-off-by: Leon Hwang --- kernel/bpf/syscall.c | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index ce349a059d5d..14fc5738f2b9 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -6211,14 +6211,35 @@ static int copy_prog_load_log_true_size(union bpf_a= ttr *attr, bpfptr_t uattr, un &attr->log_true_size); } =20 -static int copy_btf_load_log_true_size(union bpf_attr *attr, bpfptr_t uatt= r, unsigned int size) +static int check_btf_load_log_attrs(union bpf_attr *attr, struct bpf_commo= n_attr *common_attrs) +{ + int err; + + err =3D check_log_attrs(attr->btf_log_buf, attr->btf_log_size, attr->btf_= log_level, + common_attrs); + if (err) + return err; + + if (!attr->btf_log_buf && common_attrs->log_buf) { + attr->btf_log_buf =3D common_attrs->log_buf; + attr->btf_log_size =3D common_attrs->log_size; + attr->btf_log_level =3D common_attrs->log_level; + } + + return 0; +} + +static int copy_btf_load_log_true_size(union bpf_attr *attr, bpfptr_t uatt= r, unsigned int size, + struct bpf_common_attr *common_attrs, bpfptr_t uattr_common, + unsigned int size_common) { if (size >=3D offsetofend(union bpf_attr, btf_log_true_size) && copy_to_bpfptr_offset(uattr, offsetof(union bpf_attr, btf_log_true_si= ze), &attr->btf_log_true_size, sizeof(attr->btf_log_true_size))) return -EFAULT; =20 - return 0; + return copy_common_attr_log_true_size(uattr_common, size_common, + &attr->btf_log_true_size); } =20 static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size, @@ -6328,9 +6349,13 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uatt= r, unsigned int size, err =3D bpf_raw_tracepoint_open(&attr); break; case BPF_BTF_LOAD: + err =3D check_btf_load_log_attrs(&attr, &common_attrs); + if (err) + break; attr.btf_log_true_size =3D 0; err =3D bpf_btf_load(&attr, uattr); - ret =3D copy_btf_load_log_true_size(&attr, uattr, size); + ret =3D copy_btf_load_log_true_size(&attr, uattr, size, &common_attrs, u= attr_common, + size_common); err =3D ret ? ret : err; break; case BPF_BTF_GET_FD_BY_ID: --=20 2.52.0 From nobody Sat Feb 7 17:55:33 2026 Received: from out-173.mta0.migadu.com (out-173.mta0.migadu.com [91.218.175.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0943634F485 for ; Tue, 6 Jan 2026 17:21:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767720118; cv=none; b=QNHi/m82x7LvOfqcRUfrKKy1BpJLeD5osv5QULo2LYzYs7ySKCteKEVALxcnPyLMLBI7k63zij81gjwd1c9L59qxeXrnqFyzuLapvzcBXZbXMUUIykMb1igNZqfOZpiRZRYNHuzspVJhX/PMlEguR9FObLqXScHH4k6B2UhKVGY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767720118; c=relaxed/simple; bh=AETHN+3kFlVaYquHWZbaQLeMcf8iQflBHGOXp5wQCqo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dd3iRL7nnIFEuRLsF00N50/PqNHBMBjytrUlhw1q6Az9wHq+/hI8xuR1EJmLEvlI3yjzU9GhDqY1J0k11j0qnRWUqGeXiRlVvWwLRTKRlmbYJ7HZLnolLfwGxhdDv5MbMlRGMGC7Al6mk0VqA/3GE4P/kBiynJxDNwgT/sio5Ek= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=QdaEnI2+; arc=none smtp.client-ip=91.218.175.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="QdaEnI2+" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1767720113; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5mauyZYzayXq6OYND+1vFeaYe2URO7AKanu1AXWjwEU=; b=QdaEnI2+xAQi2FlSNHjuDzvhP21UVE0kcsOG6LIZgJtB0NyFGGIwfu/wlGmgEob5pxER7X Qwvf0K+jr/zXhghujm1c76640gyvD0EbejUYLp4HMGUQEa9qvhsXBl+b2jIsuX10bZ/LfM VSTZcUnOOcJTQ/eaxqp4aEEWLsao3n4= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , Christian Brauner , Oleg Nesterov , Leon Hwang , Seth Forshee , Yuichiro Tsuji , Andrey Albershteyn , Willem de Bruijn , Jason Xing , Paul Chaignon , Mykyta Yatsenko , Kumar Kartikeya Dwivedi , Anton Protopopov , Amery Hung , Rong Tao , linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, linux-kselftest@vger.kernel.org, kernel-patches-bot@fb.com Subject: [RESEND PATCH bpf-next v4 7/9] bpf: Add common attr support for map_create Date: Wed, 7 Jan 2026 01:20:16 +0800 Message-ID: <20260106172018.57757-8-leon.hwang@linux.dev> In-Reply-To: <20260106172018.57757-1-leon.hwang@linux.dev> References: <20260106172018.57757-1-leon.hwang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Currently, many 'BPF_MAP_CREATE' failures return '-EINVAL' without providing any explanation to user space. With the extended BPF syscall support, 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 --- kernel/bpf/syscall.c | 88 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 79 insertions(+), 9 deletions(-) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 14fc5738f2b9..e64cc7504731 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -1366,23 +1366,72 @@ static bool bpf_net_capable(void) return capable(CAP_NET_ADMIN) || capable(CAP_SYS_ADMIN); } =20 +struct bpf_vlog_wrapper { + struct bpf_common_attr *attr; + struct bpf_verifier_log *log; +}; + +static void bpf_vlog_wrapper_destructor(struct bpf_vlog_wrapper *w) +{ + if (!w->log) + return; + + (void) bpf_vlog_finalize(w->log, &w->attr->log_true_size); + kfree(w->log); +} + +#define DEFINE_BPF_VLOG_WRAPPER(name, common_attrs) \ + struct bpf_vlog_wrapper name __cleanup(bpf_vlog_wrapper_destructor) =3D {= \ + .attr =3D common_attrs, \ + } + +static int bpf_vlog_wrapper_init(struct bpf_vlog_wrapper *w) +{ + struct bpf_common_attr *attr =3D w->attr; + struct bpf_verifier_log *log; + int err; + + if (!attr->log_buf) + return 0; + + log =3D kzalloc(sizeof(*log), GFP_KERNEL); + if (!log) + return -ENOMEM; + + err =3D bpf_vlog_init(log, attr->log_level, u64_to_user_ptr(attr->log_buf= ), attr->log_size); + if (err) { + kfree(log); + return err; + } + + w->log =3D log; + return 0; +} + #define BPF_MAP_CREATE_LAST_FIELD excl_prog_hash_size /* called via syscall */ -static int map_create(union bpf_attr *attr, bpfptr_t uattr) +static int map_create(union bpf_attr *attr, bpfptr_t uattr, struct bpf_com= mon_attr *common_attrs) { const struct bpf_map_ops *ops; struct bpf_token *token =3D NULL; int numa_node =3D bpf_map_attr_numa_node(attr); u32 map_type =3D attr->map_type; + struct bpf_verifier_log *log; struct bpf_map *map; bool token_flag; int f_flags; int err; + DEFINE_BPF_VLOG_WRAPPER(log_wrapper, common_attrs); =20 err =3D CHECK_ATTR(BPF_MAP_CREATE); if (err) return -EINVAL; =20 + err =3D bpf_vlog_wrapper_init(&log_wrapper); + if (err) + return err; + log =3D log_wrapper.log; + /* 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 */ @@ -1390,17 +1439,25 @@ static int map_create(union bpf_attr *attr, bpfptr_= t uattr) attr->map_flags &=3D ~BPF_F_TOKEN_FD; =20 if (attr->btf_vmlinux_value_type_id) { - if (attr->map_type !=3D BPF_MAP_TYPE_STRUCT_OPS || - attr->btf_key_type_id || attr->btf_value_type_id) + if (attr->map_type !=3D BPF_MAP_TYPE_STRUCT_OPS) { + bpf_log(log, "btf_vmlinux_value_type_id can only be used with struct_op= s maps.\n"); return -EINVAL; + } + if (attr->btf_key_type_id || attr->btf_value_type_id) { + bpf_log(log, "btf_vmlinux_value_type_id is mutually exclusive with btf_= key_type_id and btf_value_type_id.\n"); + return -EINVAL; + } } else if (attr->btf_key_type_id && !attr->btf_value_type_id) { + bpf_log(log, "Invalid btf_value_type_id.\n"); return -EINVAL; } =20 if (attr->map_type !=3D BPF_MAP_TYPE_BLOOM_FILTER && attr->map_type !=3D BPF_MAP_TYPE_ARENA && - attr->map_extra !=3D 0) + attr->map_extra !=3D 0) { + bpf_log(log, "Invalid map_extra.\n"); return -EINVAL; + } =20 f_flags =3D bpf_get_file_flag(attr->map_flags); if (f_flags < 0) @@ -1408,13 +1465,17 @@ static int map_create(union bpf_attr *attr, bpfptr_= t uattr) =20 if (numa_node !=3D NUMA_NO_NODE && ((unsigned int)numa_node >=3D nr_node_ids || - !node_online(numa_node))) + !node_online(numa_node))) { + bpf_log(log, "Invalid numa_node.\n"); return -EINVAL; + } =20 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */ map_type =3D attr->map_type; - if (map_type >=3D ARRAY_SIZE(bpf_map_types)) + if (map_type >=3D ARRAY_SIZE(bpf_map_types)) { + bpf_log(log, "Invalid map_type.\n"); return -EINVAL; + } map_type =3D array_index_nospec(map_type, ARRAY_SIZE(bpf_map_types)); ops =3D bpf_map_types[map_type]; if (!ops) @@ -1432,8 +1493,10 @@ static int map_create(union bpf_attr *attr, bpfptr_t= uattr) =20 if (token_flag) { token =3D bpf_token_get_from_fd(attr->map_token_fd); - if (IS_ERR(token)) + if (IS_ERR(token)) { + bpf_log(log, "Invalid map_token_fd.\n"); return PTR_ERR(token); + } =20 /* if current token doesn't grant map creation permissions, * then we can't use this token, so ignore it and rely on @@ -1516,8 +1579,10 @@ static int map_create(union bpf_attr *attr, bpfptr_t= uattr) =20 err =3D 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; + } =20 preempt_disable(); map->cookie =3D gen_cookie_next(&bpf_map_cookie); @@ -1540,6 +1605,7 @@ static int map_create(union bpf_attr *attr, bpfptr_t = uattr) =20 btf =3D btf_get_by_fd(attr->btf_fd); if (IS_ERR(btf)) { + bpf_log(log, "Invalid btf_fd.\n"); err =3D PTR_ERR(btf); goto free_map; } @@ -6279,7 +6345,11 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uatt= r, unsigned int size, =20 switch (cmd) { case BPF_MAP_CREATE: - err =3D map_create(&attr, uattr); + common_attrs.log_true_size =3D 0; + err =3D map_create(&attr, uattr, &common_attrs); + ret =3D copy_common_attr_log_true_size(uattr_common, size_common, + &common_attrs.log_true_size); + err =3D ret ? ret : err; break; case BPF_MAP_LOOKUP_ELEM: err =3D map_lookup_elem(&attr); --=20 2.52.0 From nobody Sat Feb 7 17:55:33 2026 Received: from out-189.mta0.migadu.com (out-189.mta0.migadu.com [91.218.175.189]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D3ED23502A9 for ; Tue, 6 Jan 2026 17:22:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.189 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767720127; cv=none; b=HUjijeYwQk/hPV7SpdH0jI1A988AtaRKHcbcRjKRQJfqRtztjD7Y8DfLHRs+3A4ebcZgjjKbT0qEQVVgrb3GV8C9V+2gwgcjxYT9CmIfv542dQmPlKR1h0xrbK8axDiHbPvzMASGuEmZddSP0pNibb3bKVAExGSEioP7KP24grs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767720127; c=relaxed/simple; bh=HxI4+nzQuzg70aSRlSj9KKaRq21uALuyDmmimZAfIfI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=E+2VRaODYNanfnqK/++QhzKgx+KOW3YLLiEmegGYcPIywVMDX387JGXpGBVEapUPVSoLSJhY899Hc8zJOmSafuK7RHuaXpFy+/pEOInsb3Of5kV2Km7ws75gLUpAVKCteRrGsB51up4Tw/rLpCqvU61yS+3PYBQ4M3fawrQHmUM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=A3x0HJVr; arc=none smtp.client-ip=91.218.175.189 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="A3x0HJVr" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1767720123; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mSPAXTe59AXqGHWDQ1uMbUsQxBwsOMvl1BeG421F/78=; b=A3x0HJVrxScEAlTcSIG2ScmXBWFPoL8hCdG9KeFsqmeMKN4ms+ZAsaB9T5hSD/DJfMS2/P +ElCGOriVYdSUPvMfdh2oPLuohKcw8t7dN47czjtJA3obQovjwklGy8PsyCg6f/HjpydsI LLAT0bxLCCpF8rhkx2rLuSLMmkFUaWg= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , Christian Brauner , Oleg Nesterov , Leon Hwang , Seth Forshee , Yuichiro Tsuji , Andrey Albershteyn , Willem de Bruijn , Jason Xing , Paul Chaignon , Mykyta Yatsenko , Kumar Kartikeya Dwivedi , Anton Protopopov , Amery Hung , Rong Tao , linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, linux-kselftest@vger.kernel.org, kernel-patches-bot@fb.com Subject: [RESEND PATCH bpf-next v4 8/9] libbpf: Add common attr support for map_create Date: Wed, 7 Jan 2026 01:20:17 +0800 Message-ID: <20260106172018.57757-9-leon.hwang@linux.dev> In-Reply-To: <20260106172018.57757-1-leon.hwang@linux.dev> References: <20260106172018.57757-1-leon.hwang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" With the previous commit 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. Introduce a new struct bpf_syscall_common_attr_opts, which wraps these new fields, 'log_buf', 'log_size', 'log_level', and 'log_true_size'. Extend 'bpf_map_create_opts' with a new field, 'cattr_opts', allowing users to capture and inspect those log messages. Signed-off-by: Leon Hwang --- tools/lib/bpf/bpf.c | 15 ++++++++++++++- tools/lib/bpf/bpf.h | 17 ++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index 689ade4a822b..4a1ba4951785 100644 --- a/tools/lib/bpf/bpf.c +++ b/tools/lib/bpf/bpf.c @@ -207,6 +207,9 @@ int bpf_map_create(enum bpf_map_type map_type, const struct bpf_map_create_opts *opts) { const size_t attr_sz =3D offsetofend(union bpf_attr, excl_prog_hash_size); + const size_t common_attrs_sz =3D sizeof(struct bpf_common_attr); + struct bpf_syscall_common_attr_opts *cattr_opts; + struct bpf_common_attr common_attrs; union bpf_attr attr; int fd; =20 @@ -240,7 +243,17 @@ int bpf_map_create(enum bpf_map_type map_type, attr.excl_prog_hash =3D ptr_to_u64(OPTS_GET(opts, excl_prog_hash, NULL)); attr.excl_prog_hash_size =3D OPTS_GET(opts, excl_prog_hash_size, 0); =20 - fd =3D sys_bpf_fd(BPF_MAP_CREATE, &attr, attr_sz); + cattr_opts =3D OPTS_GET(opts, cattr_opts, NULL); + if (cattr_opts && feat_supported(NULL, FEAT_EXTENDED_SYSCALL)) { + memset(&common_attrs, 0, common_attrs_sz); + common_attrs.log_buf =3D ptr_to_u64(OPTS_GET(cattr_opts, log_buf, NULL)); + common_attrs.log_size =3D OPTS_GET(cattr_opts, log_size, 0); + common_attrs.log_level =3D OPTS_GET(cattr_opts, log_level, 0); + fd =3D sys_bpf_ext_fd(BPF_MAP_CREATE, &attr, attr_sz, &common_attrs, com= mon_attrs_sz); + OPTS_SET(cattr_opts, log_true_size, common_attrs.log_true_size); + } else { + fd =3D sys_bpf_fd(BPF_MAP_CREATE, &attr, attr_sz); + } return libbpf_err_errno(fd); } =20 diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h index 1f9c28d27795..6f544c205b89 100644 --- a/tools/lib/bpf/bpf.h +++ b/tools/lib/bpf/bpf.h @@ -37,6 +37,18 @@ extern "C" { =20 LIBBPF_API int libbpf_set_memlock_rlim(size_t memlock_bytes); =20 +struct bpf_syscall_common_attr_opts { + size_t sz; /* size of this struct for forward/backward compatibility */ + + const char *log_buf; + __u32 log_size; + __u32 log_level; + __u32 log_true_size; + + size_t :0; +}; +#define bpf_syscall_common_attr_opts__last_field log_true_size + struct bpf_map_create_opts { size_t sz; /* size of this struct for forward/backward compatibility */ =20 @@ -57,9 +69,12 @@ struct bpf_map_create_opts { =20 const void *excl_prog_hash; __u32 excl_prog_hash_size; + + struct bpf_syscall_common_attr_opts *cattr_opts; + size_t :0; }; -#define bpf_map_create_opts__last_field excl_prog_hash_size +#define bpf_map_create_opts__last_field cattr_opts =20 LIBBPF_API int bpf_map_create(enum bpf_map_type map_type, const char *map_name, --=20 2.52.0 From nobody Sat Feb 7 17:55:33 2026 Received: from out-182.mta0.migadu.com (out-182.mta0.migadu.com [91.218.175.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 78F2A350D57 for ; Tue, 6 Jan 2026 17:22:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767720138; cv=none; b=GUi2xmsB7SILy3WHsCb9f4ZEChMI3icyQfYJzZSwwX2/7a+y68upT86hrZd9v89E3XQtYsGvg/Lc/hEY70lKtneggOos+WFdQtQCSwmK1SrZEp3YU3EtzMtXRjax815YzICLdeQq0qrba3I9tSZFrx3CxONrJYXE63vWJWXdYhc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767720138; c=relaxed/simple; bh=MAXHjOZ2vDeALsrSrzEPhCR/6gju9wZ2EZGKEhw6FRs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SPstI79xblJAFzleX3RlVOspD3MNCjk+4LoZXxLtmu7fj/qlwwQMle75cOp0erkxVRM6FYulMT6YpYVS5CQJg7eQ/7y8B97lDm2i2bJCSd0t2ZzjQTOkd2ABOKV6mw9dVPFMAUxbn65g4qCx/Wod3UrSD+/p/1ORQaUbKgBTe/w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=BHB/Ap6K; arc=none smtp.client-ip=91.218.175.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="BHB/Ap6K" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1767720133; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8nQ8vlpZLZ5VvbTZ6i9ChLmZ/pLF0l+ICk23bpsc6Vs=; b=BHB/Ap6KtyI5TVmKGrT6iNUzOQCaSXYwhBOh8hg6YVDSGmKLlB/4aNEHyGe7mWw8XC38NV o1F+TCZ/vT8E4SL42MJybXrnsK/i8t6s0UDyBZ3tJa+FlsMA2XR/LO63A3vZVZ2Zg3RYQQ pvqeN4JLCzQqqpgodutY/nBOCZCGWQk= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , Christian Brauner , Oleg Nesterov , Leon Hwang , Seth Forshee , Yuichiro Tsuji , Andrey Albershteyn , Willem de Bruijn , Jason Xing , Paul Chaignon , Mykyta Yatsenko , Kumar Kartikeya Dwivedi , Anton Protopopov , Amery Hung , Rong Tao , linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, linux-kselftest@vger.kernel.org, kernel-patches-bot@fb.com Subject: [RESEND PATCH bpf-next v4 9/9] selftests/bpf: Add tests to verify map create failure log Date: Wed, 7 Jan 2026 01:20:18 +0800 Message-ID: <20260106172018.57757-10-leon.hwang@linux.dev> In-Reply-To: <20260106172018.57757-1-leon.hwang@linux.dev> References: <20260106172018.57757-1-leon.hwang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" As kernel is able to report log when fail to create map, add tests to verify those logs. Signed-off-by: Leon Hwang --- .../selftests/bpf/prog_tests/map_init.c | 143 ++++++++++++++++++ 1 file changed, 143 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/map_init.c b/tools/test= ing/selftests/bpf/prog_tests/map_init.c index 14a31109dd0e..52bd4b1966c8 100644 --- a/tools/testing/selftests/bpf/prog_tests/map_init.c +++ b/tools/testing/selftests/bpf/prog_tests/map_init.c @@ -212,3 +212,146 @@ void test_map_init(void) if (test__start_subtest("pcpu_lru_map_init")) test_pcpu_lru_map_init(); } + +#define BPF_LOG_FIXED 8 + +static void test_map_create(enum bpf_map_type map_type, const char *map_na= me, + struct bpf_map_create_opts *opts, const char *exp_msg) +{ + const int key_size =3D 4, value_size =3D 4, max_entries =3D 1; + char log_buf[128]; + int fd; + LIBBPF_OPTS(bpf_syscall_common_attr_opts, copts); + + log_buf[0] =3D '\0'; + copts.log_buf =3D log_buf; + copts.log_size =3D sizeof(log_buf); + copts.log_level =3D BPF_LOG_FIXED; + opts->cattr_opts =3D &copts; + fd =3D bpf_map_create(map_type, map_name, key_size, value_size, max_entri= es, opts); + if (!ASSERT_LT(fd, 0, "bpf_map_create")) { + close(fd); + return; + } + + ASSERT_STREQ(log_buf, exp_msg, "log_buf"); + ASSERT_EQ(copts.log_true_size, strlen(exp_msg) + 1, "log_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 =3D "btf_vmlinux_value_type_id can only be used with stru= ct_ops maps.\n"; + LIBBPF_OPTS(bpf_map_create_opts, opts, + .btf_vmlinux_value_type_id =3D 1, + ); + + test_map_create_array(&opts, msg); +} + +static void test_invalid_vmlinux_value_type_id_kv_type_id(void) +{ + const char *msg =3D "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 =3D 1, + .btf_key_type_id =3D 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 =3D "Invalid btf_value_type_id.\n"; + LIBBPF_OPTS(bpf_map_create_opts, opts, + .btf_key_type_id =3D 1, + ); + + test_map_create_array(&opts, msg); +} + +static void test_invalid_map_extra(void) +{ + const char *msg =3D "Invalid map_extra.\n"; + LIBBPF_OPTS(bpf_map_create_opts, opts, + .map_extra =3D 1, + ); + + test_map_create_array(&opts, msg); +} + +static void test_invalid_numa_node(void) +{ + const char *msg =3D "Invalid numa_node.\n"; + LIBBPF_OPTS(bpf_map_create_opts, opts, + .map_flags =3D BPF_F_NUMA_NODE, + .numa_node =3D 0xFF, + ); + + test_map_create_array(&opts, msg); +} + +static void test_invalid_map_type(void) +{ + const char *msg =3D "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 =3D "Invalid map_token_fd.\n"; + LIBBPF_OPTS(bpf_map_create_opts, opts, + .map_flags =3D BPF_F_TOKEN_FD, + .token_fd =3D 0xFF, + ); + + test_map_create_array(&opts, msg); +} + +static void test_invalid_map_name(void) +{ + const char *msg =3D "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 =3D "Invalid btf_fd.\n"; + LIBBPF_OPTS(bpf_map_create_opts, opts, + .btf_fd =3D -1, + .btf_key_type_id =3D 1, + .btf_value_type_id =3D 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(); +} --=20 2.52.0