From nobody Mon Jan 26 22:44:37 2026 Received: from out-174.mta0.migadu.com (out-174.mta0.migadu.com [91.218.175.174]) (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 DF5EF33CEB7 for ; Mon, 26 Jan 2026 15:14:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769440491; cv=none; b=SpSlYgD+bfE2656WFLmKlq0iC6kvKqxvzL7uPBZCY6/QqEGnyB+1U5VbA7toa2JgAclWuMyFoz8P3pDDt+3kTnOj+tcs1KSfX/KUIF+5z4gcsctDL6MjPmBc4PIIjoxaWbet7b8pKZWOZxzxgyarCM1rhIMzqh9iuDv0tEAC5Qo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769440491; c=relaxed/simple; bh=EfDci4aAKrGFTvZ1ZqjNhcJ4BLNu5tXigdVPS+fkkrE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=F5kzE00y3bHt0/Que5rp5WygWkN0BWRJaLldJlgnYMSiVVCfpw2taKkBUT7BJovzpj0BSU3uJwa5rb1CXKSgrlTFU/g5mJ+sCj1P1QWGVE1zBzEXhtB2nVCEgOnu8DxtvFZEFjStwntJAOpaBm8/4EJvEQU6uy6hENQ6H6fNQTc= 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=AUQSoQKC; arc=none smtp.client-ip=91.218.175.174 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="AUQSoQKC" 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=1769440486; 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=kBQU738wkB0LE2d5CWz2sTQKdf2BsoSEej9rnF/lOwI=; b=AUQSoQKC8CVM2j+LC0CxsNjJNVarUwhgx9bGLacU4wiR94BJfYFnFP4h3I9PWB6S7fOafH YrYHuqBlhpDwocNznGB8t8zQC8EYHXh8UX5qwuEbj7lK9qxx6LHx00+JTYgBnwY3OwcsMc yRF/3CZP6m/GVZb0flqp0JEBmc/2W/g= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , Christian Brauner , Seth Forshee , Yuichiro Tsuji , Andrey Albershteyn , Leon Hwang , Willem de Bruijn , Jason Xing , Tao Chen , 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: [PATCH bpf-next v8 1/9] bpf: Extend BPF syscall with common attributes support Date: Mon, 26 Jan 2026 23:14:01 +0800 Message-ID: <20260126151409.52072-2-leon.hwang@linux.dev> In-Reply-To: <20260126151409.52072-1-leon.hwang@linux.dev> References: <20260126151409.52072-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 44e7dbc278e3..656757e7a4fb 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 syscall common attrs. = */ }; =20 enum bpf_map_type { @@ -1492,6 +1493,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 b9184545c3fd..69bfcffb4389 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -6179,8 +6179,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 attr_common; union bpf_attr attr; int err; =20 @@ -6194,6 +6196,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(&attr_common, 0, sizeof(attr_common)); + if (cmd & BPF_COMMON_ATTRS) { + err =3D bpf_check_uarg_tail_zero(uattr_common, sizeof(attr_common), size= _common); + if (err) + return err; + + cmd &=3D ~BPF_COMMON_ATTRS; + size_common =3D min_t(u32, size_common, sizeof(attr_common)); + if (copy_from_bpfptr(&attr_common, 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; @@ -6329,9 +6345,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, @@ -6362,7 +6379,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 3ca7d76e05f0..39022e07f6fd 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 syscall common attrs. = */ }; =20 enum bpf_map_type { @@ -1492,6 +1493,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 Mon Jan 26 22:44:37 2026 Received: from out-172.mta0.migadu.com (out-172.mta0.migadu.com [91.218.175.172]) (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 C255925A64C for ; Mon, 26 Jan 2026 15:14:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769440501; cv=none; b=cJY8Pw2NI7SsBxTvEdHyp3mcq+ruqX2V9XUAARM14Em8ZBZUTAnKwxtPzo7QCHMgbeeRjgW62/n57YvhHnhMnZS4Kj8WeNEMIZzHUmWukpCkKn4w5gBUioapP7G0WWSOwV7bcrAug/V2sfu12k/ler8AEO/xTfXMqZ0fuddTh8w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769440501; c=relaxed/simple; bh=zaHeGo39l7aEuaZEfB56y1SA/o4IPg0uaFbgQja2DE8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gH4rDkqBCp7Vub7CUmSeI95dcFtF0pH491aRMnyRvTZ3lwzA3OJlMcvqtmUUFlArRjFuHwRz+3cV/NxeGhwzIMEKEkX3TKVUOCKgXoAF/hEvX/dJljOfnoA7e9cU9OI4f9u5LEO6/1qS0U6HdbeZ++QdYJ1EVBVfXpJtFva8n4U= 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=O4vfHn09; arc=none smtp.client-ip=91.218.175.172 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="O4vfHn09" 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=1769440495; 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=gu+RSjY5ZXq9VFl4b21GzQKsHQDjqCZoGYucoG6n0Lw=; b=O4vfHn09K8mMmo3N7R0TIqLf3BTs9q1kV2fE/sgHpGfLvngO8INV5qF5WQbGNE13JeQ2B0 1VnqyoApwXBBlXrPKF7khuQ+uCcfcfswWziWfzNqgKHLpYdHcrD3+g3pVN5Jq5dkEnJQkE LMfbhXWJQYFXIdVGoqx1St1r1nWuOu4= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , Christian Brauner , Seth Forshee , Yuichiro Tsuji , Andrey Albershteyn , Leon Hwang , Willem de Bruijn , Jason Xing , Tao Chen , 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: [PATCH bpf-next v8 2/9] libbpf: Add support for extended bpf syscall Date: Mon, 26 Jan 2026 23:14:02 +0800 Message-ID: <20260126151409.52072-3-leon.hwang@linux.dev> In-Reply-To: <20260126151409.52072-1-leon.hwang@linux.dev> References: <20260126151409.52072-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 BPF syscall common attributes. Signed-off-by: Leon Hwang --- tools/lib/bpf/bpf.c | 36 +++++++++++++++++++++++++++++++++ tools/lib/bpf/features.c | 8 ++++++++ tools/lib/bpf/libbpf_internal.h | 3 +++ 3 files changed, 47 insertions(+) diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index 5846de364209..9d8740761b7a 100644 --- a/tools/lib/bpf/bpf.c +++ b/tools/lib/bpf/bpf.c @@ -69,6 +69,42 @@ 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 *attr_common, + unsigned int size_common) +{ + cmd =3D attr_common ? (cmd | BPF_COMMON_ATTRS) : (cmd & ~BPF_COMMON_ATTRS= ); + return syscall(__NR_bpf, cmd, attr, size, attr_common, size_common); +} + +static inline int sys_bpf_ext_fd(enum bpf_cmd cmd, union bpf_attr *attr, + unsigned int size, + struct bpf_common_attr *attr_common, + unsigned int size_common) +{ + int fd; + + fd =3D sys_bpf_ext(cmd, attr, size, attr_common, size_common); + return ensure_good_fd(fd); +} + +int probe_sys_bpf_ext(void) +{ + const size_t attr_sz =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 -EINVAL; + } + return errno =3D=3D EFAULT ? 1 : 0; +} + 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..e0d646a9e233 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_bpf_syscall_common_attrs(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_BPF_SYSCALL_COMMON_ATTRS] =3D { + "BPF syscall common attributes support", probe_bpf_syscall_common_attrs, + }, }; =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..aa16be869c4f 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 BPF syscall common attributes */ + FEAT_BPF_SYSCALL_COMMON_ATTRS, __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 Mon Jan 26 22:44:37 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 A63CF329E4B; Mon, 26 Jan 2026 15:15:22 +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=1769440524; cv=none; b=tWJ54uW3Lo0+dqfAPG87k4v65gmCytplC8aCKqUlKANzhq2jzZQFOJV3Y9T1yQA0NUrUwtbGZc/6+gy27pQRD8ilP3WUHpTztJJTMeSueWgCIGaiyyEnjb8FTMU1kgove7nwicaMBPRL67Cqs9UZ3mJgZKuy9ooGC/Vrp216gQs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769440524; c=relaxed/simple; bh=24JpjwoHArjgVj8g2K9Q7/aJeAHzj9JeHL163iIxtlw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jkewT9jkmzoVHtCd35+dnyzpUdt93TGlPSDo7dTRXiXOZasnEIGK/1GrorvvJzA9UeeF/mySOjqzQfX0A/wZWo2iDsMYTIYpUrBRPCBReDEbEgFnAeEuNdOfEnXKq0tVZ241zhnnRY4WmpnonAoll3MvlE0dfOC8htIZ1BiRLCI= 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=vXQ/k5OT; 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="vXQ/k5OT" 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=1769440519; 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=xkoWUagbqzjzpb/c4YR0cGdndfNVHx6z39d6yjsJlVA=; b=vXQ/k5OTx3Uv1Um7zTSWw4y153PXGu3ahxtmtv922G+JG7jqssgBp7dsBDVl8pvVVa3lPf Bt8RSfiq0VvWzwbuefOBppX9Ea++bNa/BFkKg2YuT6s4UAqlvIUAXLS2nCW+GksP6IQeQG u29d5QzA62/7GwsOpUmF3Ne/Dc4u6dA= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , Christian Brauner , Seth Forshee , Yuichiro Tsuji , Andrey Albershteyn , Leon Hwang , Willem de Bruijn , Jason Xing , Tao Chen , 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: [PATCH bpf-next v8 3/9] bpf: Refactor reporting log_true_size for prog_load Date: Mon, 26 Jan 2026 23:14:03 +0800 Message-ID: <20260126151409.52072-4-leon.hwang@linux.dev> In-Reply-To: <20260126151409.52072-1-leon.hwang@linux.dev> References: <20260126151409.52072-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 next commit will add support for reporting logs via extended common attributes, including 'log_true_size'. To prepare for that, refactor the 'log_true_size' reporting logic by introducing a new struct bpf_log_attr to encapsulate log-related behavior: * bpf_prog_load_log_attr_init(): initialize the log fields, which will support extended common attributes in the next commit. * bpf_log_attr_finalize(): handle log finalization and write back 'log_true_size' to userspace. Signed-off-by: Leon Hwang --- include/linux/bpf.h | 19 ++++++++++++++++- include/linux/bpf_verifier.h | 11 ++++++++++ kernel/bpf/log.c | 40 ++++++++++++++++++++++++++++++++++++ kernel/bpf/syscall.c | 9 +++++++- kernel/bpf/verifier.c | 19 ++++++----------- 5 files changed, 83 insertions(+), 15 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 4427c6e98331..1946f35b44fb 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -2903,8 +2903,25 @@ int bpf_get_file_flag(int flags); int bpf_check_uarg_tail_zero(bpfptr_t uaddr, size_t expected_size, size_t actual_size); =20 +struct bpf_attrs { + const void *attr; + bpfptr_t uattr; + u32 size; +}; + +static inline void bpf_attrs_init(struct bpf_attrs *attrs, const void *att= r, bpfptr_t uattr, + u32 size) +{ + memset(attrs, 0, sizeof(*attrs)); + attrs->attr =3D attr; + attrs->uattr =3D uattr; + attrs->size =3D size; +} + /* verify correctness of eBPF program */ -int bpf_check(struct bpf_prog **fp, union bpf_attr *attr, bpfptr_t uattr, = u32 uattr_size); +struct bpf_log_attr; +int bpf_check(struct bpf_prog **fp, union bpf_attr *attr, bpfptr_t uattr, + struct bpf_log_attr *log_attr); =20 #ifndef CONFIG_BPF_JIT_ALWAYS_ON void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth); diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index 8355b585cd18..4a0c5ef296b9 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -631,6 +631,17 @@ static inline bool bpf_verifier_log_needed(const struc= t bpf_verifier_log *log) return log && log->level; } =20 +struct bpf_log_attr { + u64 log_buf; + u32 log_size; + u32 log_level; + struct bpf_attrs *attrs; + u32 offsetof_log_true_size; +}; + +int bpf_prog_load_log_attr_init(struct bpf_log_attr *log_attr, struct bpf_= attrs *attrs); +int bpf_log_attr_finalize(struct bpf_log_attr *log_attr, struct bpf_verifi= er_log *log); + #define BPF_MAX_SUBPROGS 256 =20 struct bpf_subprog_arg_info { diff --git a/kernel/bpf/log.c b/kernel/bpf/log.c index a0c3b35de2ce..457b724c4176 100644 --- a/kernel/bpf/log.c +++ b/kernel/bpf/log.c @@ -863,3 +863,43 @@ void print_insn_state(struct bpf_verifier_env *env, co= nst struct bpf_verifier_st } print_verifier_state(env, vstate, frameno, false); } + +static int bpf_log_attr_init(struct bpf_log_attr *log_attr, struct bpf_att= rs *attrs, u64 log_buf, + u32 log_size, u32 log_level, int offsetof_log_true_size) +{ + memset(log_attr, 0, sizeof(*log_attr)); + log_attr->log_buf =3D log_buf; + log_attr->log_size =3D log_size; + log_attr->log_level =3D log_level; + log_attr->attrs =3D attrs; + log_attr->offsetof_log_true_size =3D offsetof_log_true_size; + return 0; +} + +int bpf_prog_load_log_attr_init(struct bpf_log_attr *log_attr, struct bpf_= attrs *attrs) +{ + const union bpf_attr *attr =3D attrs->attr; + + return bpf_log_attr_init(log_attr, attrs, attr->log_buf, attr->log_size, = attr->log_level, + offsetof(union bpf_attr, log_true_size)); +} + +int bpf_log_attr_finalize(struct bpf_log_attr *log_attr, struct bpf_verifi= er_log *log) +{ + u32 log_true_size, off; + size_t size; + int err; + + if (!log) + return 0; + + err =3D bpf_vlog_finalize(log, &log_true_size); + + size =3D sizeof(log_true_size); + off =3D log_attr->offsetof_log_true_size; + if (log_attr->attrs && log_attr->attrs->size >=3D off + size && + copy_to_bpfptr_offset(log_attr->attrs->uattr, off, &log_true_size, si= ze)) + err =3D -EFAULT; + + return err; +} diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 69bfcffb4389..d422664e00dd 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -2871,6 +2871,8 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr= _t uattr, u32 uattr_size) struct bpf_prog *prog, *dst_prog =3D NULL; struct btf *attach_btf =3D NULL; struct bpf_token *token =3D NULL; + struct bpf_log_attr log_attr; + struct bpf_attrs attrs; bool bpf_cap; int err; char license[128]; @@ -3082,8 +3084,13 @@ static int bpf_prog_load(union bpf_attr *attr, bpfpt= r_t uattr, u32 uattr_size) if (err) goto free_prog_sec; =20 + bpf_attrs_init(&attrs, attr, uattr, uattr_size); + err =3D bpf_prog_load_log_attr_init(&log_attr, &attrs); + if (err < 0) + goto free_used_maps; + /* run eBPF verifier */ - err =3D bpf_check(&prog, attr, uattr, uattr_size); + err =3D bpf_check(&prog, attr, uattr, &log_attr); if (err < 0) goto free_used_maps; =20 diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index c2f2650db9fd..134871f46afb 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -25643,12 +25643,12 @@ 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, + struct bpf_log_attr *log_attr) { 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); @@ -25695,9 +25695,9 @@ int bpf_check(struct bpf_prog **prog, union bpf_att= r *attr, bpfptr_t uattr, __u3 /* user could have requested verbose verifier output * and supplied buffer to store the verification trace */ - ret =3D bpf_vlog_init(&env->log, attr->log_level, - (char __user *) (unsigned long) attr->log_buf, - attr->log_size); + ret =3D bpf_vlog_init(&env->log, log_attr->log_level, + u64_to_user_ptr(log_attr->log_buf), + log_attr->log_size); if (ret) goto err_unlock; =20 @@ -25847,17 +25847,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_log_attr_finalize(log_attr, &env->log); 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 Mon Jan 26 22:44:37 2026 Received: from out-170.mta0.migadu.com (out-170.mta0.migadu.com [91.218.175.170]) (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 E787C329E4B for ; Mon, 26 Jan 2026 15:15:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769440531; cv=none; b=sQIgDp2+LkPl8BCHDVf/AO2UQR5KDK9j7MzzWt4AhgCdMufOlxNYvQDIansuivatyd9f3mHBftAGamyXmI5BykRvwgHfNQKARFLC8zd54stqfsNePL6YCpu/8GSzIb94EH9G6v/9tB59JW1bPGwQl+vCPGzVTHn0rYfaAajZ19Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769440531; c=relaxed/simple; bh=n73GNLF5hUUtTlNZBHjxfVWGz/aHm7xtBCqlxGzf/5g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Zq1oiqX28rrMOQQiIevZW55/4ALw4MbXejQ4qL5mzEgnMJ8ZlZQHjYA+m+s/zXyAHZ5RXyPQS2IKxdC8lZp2BArCrmQyZjUvkkNZICOk3z63yToC+vmcplc2+QHo52OrbSxoinrxLQUc1FRSrB6MyEbmYiKuNk0fgcurrMsGOEM= 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=qgpVLBWp; arc=none smtp.client-ip=91.218.175.170 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="qgpVLBWp" 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=1769440527; 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=bqMAK62FUEdGritvnuOWR/1nXfdD5l2yLQw1KVjWJ84=; b=qgpVLBWpAn9FlwsErx2xWxOlgWnntVZaxKwUBAcn5ewz6OeZPBdGaeNde1gePy6AtWmMYF SLa/x+szc4/rxQv39TDYz1E0NwKjb9YrGjI5wF6VjlYJ0HC5vNMuQZes8d09Y9kuSoU9Gq R39osF5hSXZYOfbS97Pje5KX8DhbThA= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , Christian Brauner , Seth Forshee , Yuichiro Tsuji , Andrey Albershteyn , Leon Hwang , Willem de Bruijn , Jason Xing , Tao Chen , 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: [PATCH bpf-next v8 4/9] bpf: Add syscall common attributes support for prog_load Date: Mon, 26 Jan 2026 23:14:04 +0800 Message-ID: <20260126151409.52072-5-leon.hwang@linux.dev> In-Reply-To: <20260126151409.52072-1-leon.hwang@linux.dev> References: <20260126151409.52072-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 -EINVAL. Signed-off-by: Leon Hwang --- include/linux/bpf_verifier.h | 4 +++- kernel/bpf/log.c | 29 ++++++++++++++++++++++++++--- kernel/bpf/syscall.c | 9 ++++++--- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index 4a0c5ef296b9..7eb024e83d2d 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -637,9 +637,11 @@ struct bpf_log_attr { u32 log_level; struct bpf_attrs *attrs; u32 offsetof_log_true_size; + struct bpf_attrs *attrs_common; }; =20 -int bpf_prog_load_log_attr_init(struct bpf_log_attr *log_attr, struct bpf_= attrs *attrs); +int bpf_prog_load_log_attr_init(struct bpf_log_attr *log_attr, struct bpf_= attrs *attrs, + struct bpf_attrs *attrs_common); int bpf_log_attr_finalize(struct bpf_log_attr *log_attr, struct bpf_verifi= er_log *log); =20 #define BPF_MAX_SUBPROGS 256 diff --git a/kernel/bpf/log.c b/kernel/bpf/log.c index 457b724c4176..c0b816e84384 100644 --- a/kernel/bpf/log.c +++ b/kernel/bpf/log.c @@ -865,23 +865,41 @@ void print_insn_state(struct bpf_verifier_env *env, c= onst struct bpf_verifier_st } =20 static int bpf_log_attr_init(struct bpf_log_attr *log_attr, struct bpf_att= rs *attrs, u64 log_buf, - u32 log_size, u32 log_level, int offsetof_log_true_size) + u32 log_size, u32 log_level, int offsetof_log_true_size, + struct bpf_attrs *attrs_common) { + const struct bpf_common_attr *common =3D attrs_common ? attrs_common->att= r : NULL; + memset(log_attr, 0, sizeof(*log_attr)); log_attr->log_buf =3D log_buf; log_attr->log_size =3D log_size; log_attr->log_level =3D log_level; log_attr->attrs =3D attrs; log_attr->offsetof_log_true_size =3D offsetof_log_true_size; + log_attr->attrs_common =3D attrs_common; + + if (log_buf && common && common->log_buf && + (log_buf !=3D common->log_buf || + log_size !=3D common->log_size || + log_level !=3D common->log_level)) + return -EINVAL; + + if (!log_buf && common && common->log_buf) { + log_attr->log_buf =3D common->log_buf; + log_attr->log_size =3D common->log_size; + log_attr->log_level =3D common->log_level; + } + return 0; } =20 -int bpf_prog_load_log_attr_init(struct bpf_log_attr *log_attr, struct bpf_= attrs *attrs) +int bpf_prog_load_log_attr_init(struct bpf_log_attr *log_attr, struct bpf_= attrs *attrs, + struct bpf_attrs *attrs_common) { const union bpf_attr *attr =3D attrs->attr; =20 return bpf_log_attr_init(log_attr, attrs, attr->log_buf, attr->log_size, = attr->log_level, - offsetof(union bpf_attr, log_true_size)); + offsetof(union bpf_attr, log_true_size), attrs_common); } =20 int bpf_log_attr_finalize(struct bpf_log_attr *log_attr, struct bpf_verifi= er_log *log) @@ -901,5 +919,10 @@ int bpf_log_attr_finalize(struct bpf_log_attr *log_att= r, struct bpf_verifier_log copy_to_bpfptr_offset(log_attr->attrs->uattr, off, &log_true_size, si= ze)) err =3D -EFAULT; =20 + off =3D offsetof(struct bpf_common_attr, log_true_size); + if (log_attr->attrs_common && log_attr->attrs_common->size >=3D off + siz= e && + copy_to_bpfptr_offset(log_attr->attrs_common->uattr, off, &log_true_s= ize, size)) + err =3D -EFAULT; + return err; } diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index d422664e00dd..3d1d1181b9b4 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -2865,7 +2865,8 @@ 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, u32 uattr_s= ize, + struct bpf_attrs *attrs_common) { enum bpf_prog_type type =3D attr->prog_type; struct bpf_prog *prog, *dst_prog =3D NULL; @@ -3085,7 +3086,7 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr= _t uattr, u32 uattr_size) goto free_prog_sec; =20 bpf_attrs_init(&attrs, attr, uattr, uattr_size); - err =3D bpf_prog_load_log_attr_init(&log_attr, &attrs); + err =3D bpf_prog_load_log_attr_init(&log_attr, &attrs, attrs_common); if (err < 0) goto free_used_maps; =20 @@ -6190,6 +6191,7 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr= , unsigned int size, bpfptr_t uattr_common, unsigned int size_common) { struct bpf_common_attr attr_common; + struct bpf_attrs attrs_common; union bpf_attr attr; int err; =20 @@ -6241,7 +6243,8 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr= , unsigned int size, err =3D map_freeze(&attr); break; case BPF_PROG_LOAD: - err =3D bpf_prog_load(&attr, uattr, size); + bpf_attrs_init(&attrs_common, &attr_common, uattr_common, size_common); + err =3D bpf_prog_load(&attr, uattr, size, &attrs_common); break; case BPF_OBJ_PIN: err =3D bpf_obj_pin(&attr); --=20 2.52.0 From nobody Mon Jan 26 22:44:37 2026 Received: from out-188.mta0.migadu.com (out-188.mta0.migadu.com [91.218.175.188]) (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 9B54933E35D for ; Mon, 26 Jan 2026 15:15:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.188 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769440541; cv=none; b=HjlY+kkq1AoXZ2j1M/WU1t6iFkp0TxMrM3xC42PTgy2DnZNr3C4lEyXBVfBwvi7M0CcHGClTLIsbeW1LTJh2dlS4airkaag+zkusOFB6phldHL3y9G9Sm9kxPaIRmucXc9l2uRQvNrTNhKcrraW+hBoGBRDCVE7KdCqH4jeV2ps= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769440541; c=relaxed/simple; bh=4tbrD08uMhhoFazxKVSiqhJ7+d0eMAt6n4MwMoGuQlk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=A0rxBEUWAGQ8mPYDnXa2eZI1q6h8BNQ1yvnYrF24q9vLN3nYdqaPDjNepNIYgJiZDmbMrljsobxJnKzRe72dLP3EDoA5kzG7wo96zvV16yOIQBu4RnuNpfHobmUoLzKy7Nxw+DC+c8KgZFLpYjA1LUk5dKeFtua+D9mpXrnFZ0Y= 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=A9NZM7iT; arc=none smtp.client-ip=91.218.175.188 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="A9NZM7iT" 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=1769440536; 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=ceLvjl4gDbLHzCPHErvlIkbN/33FzWki4lbrsWwVsCs=; b=A9NZM7iTJsM2HkDNz6vIGiFjXF31363OTstp9mBNkdftO9d2PWpRjguAeEo6q+dAb4d7/k epbRy8DuTdGXmleipfI8oBqZFYpLcQUDOhi0J7Hyyi+FPywG999KVrPo9t6CO4CuQNq/ys dQTXZKST2xchSVU0047PddnUVtgwH5U= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , Christian Brauner , Seth Forshee , Yuichiro Tsuji , Andrey Albershteyn , Leon Hwang , Willem de Bruijn , Jason Xing , Tao Chen , 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: [PATCH bpf-next v8 5/9] bpf: Refactor reporting btf_log_true_size for btf_load Date: Mon, 26 Jan 2026 23:14:05 +0800 Message-ID: <20260126151409.52072-6-leon.hwang@linux.dev> In-Reply-To: <20260126151409.52072-1-leon.hwang@linux.dev> References: <20260126151409.52072-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/bpf_verifier.h | 1 + include/linux/btf.h | 3 ++- kernel/bpf/btf.c | 32 +++++++++----------------------- kernel/bpf/log.c | 9 +++++++++ kernel/bpf/syscall.c | 10 +++++++++- 5 files changed, 30 insertions(+), 25 deletions(-) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index 7eb024e83d2d..28e22a03ac84 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -642,6 +642,7 @@ struct bpf_log_attr { =20 int bpf_prog_load_log_attr_init(struct bpf_log_attr *log_attr, struct bpf_= attrs *attrs, struct bpf_attrs *attrs_common); +int bpf_btf_load_log_attr_init(struct bpf_log_attr *log_attr, struct bpf_a= ttrs *attrs); int bpf_log_attr_finalize(struct bpf_log_attr *log_attr, struct bpf_verifi= er_log *log); =20 #define BPF_MAX_SUBPROGS 256 diff --git a/include/linux/btf.h b/include/linux/btf.h index 48108471c5b1..2812caa6c60e 100644 --- a/include/linux/btf.h +++ b/include/linux/btf.h @@ -145,7 +145,8 @@ const char *btf_get_name(const struct btf *btf); void btf_get(struct btf *btf); void btf_put(struct btf *btf); const struct btf_header *btf_header(const struct btf *btf); -int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr, u32 uattr_sz); +struct bpf_log_attr; +int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_= attr *log_attr); 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 8959f3bc1e92..3565570601e5 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -5856,25 +5856,11 @@ static int btf_check_type_tags(struct btf_verifier_= env *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(const union bpf_attr *attr, bpfptr_t uattr, + struct bpf_log_attr *log_attr) { 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); + char __user *log_ubuf =3D u64_to_user_ptr(log_attr->log_buf); struct btf_struct_metas *struct_meta_tab; struct btf_verifier_env *env =3D NULL; struct btf *btf =3D NULL; @@ -5891,8 +5877,8 @@ static struct btf *btf_parse(const union bpf_attr *at= tr, bpfptr_t uattr, u32 uat /* user could have requested verbose verifier output * and supplied buffer to store the verification trace */ - err =3D bpf_vlog_init(&env->log, attr->btf_log_level, - log_ubuf, attr->btf_log_size); + err =3D bpf_vlog_init(&env->log, log_attr->log_level, + log_ubuf, log_attr->log_size); if (err) goto errout_free; =20 @@ -5953,7 +5939,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_log_attr_finalize(log_attr, &env->log); if (err) goto errout_free; =20 @@ -5965,7 +5951,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_log_attr_finalize(log_attr, &env->log); if (ret) err =3D ret; errout_free: @@ -8136,12 +8122,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(const union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_= attr *log_attr) { struct btf *btf; int ret; =20 - btf =3D btf_parse(attr, uattr, uattr_size); + btf =3D btf_parse(attr, uattr, log_attr); if (IS_ERR(btf)) return PTR_ERR(btf); =20 diff --git a/kernel/bpf/log.c b/kernel/bpf/log.c index c0b816e84384..f1ed24157d71 100644 --- a/kernel/bpf/log.c +++ b/kernel/bpf/log.c @@ -902,6 +902,15 @@ int bpf_prog_load_log_attr_init(struct bpf_log_attr *l= og_attr, struct bpf_attrs offsetof(union bpf_attr, log_true_size), attrs_common); } =20 +int bpf_btf_load_log_attr_init(struct bpf_log_attr *log_attr, struct bpf_a= ttrs *attrs) +{ + const union bpf_attr *attr =3D attrs->attr; + + return bpf_log_attr_init(log_attr, attrs, attr->btf_log_buf, attr->btf_lo= g_size, + attr->btf_log_level, offsetof(union bpf_attr, btf_log_true_size), + NULL); +} + int bpf_log_attr_finalize(struct bpf_log_attr *log_attr, struct bpf_verifi= er_log *log) { u32 log_true_size, off; diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 3d1d1181b9b4..1ed007511776 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -5452,6 +5452,9 @@ static int bpf_obj_get_info_by_fd(const union bpf_att= r *attr, static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr, __u32 = uattr_size) { struct bpf_token *token =3D NULL; + struct bpf_log_attr log_attr; + struct bpf_attrs attrs; + int err; =20 if (CHECK_ATTR(BPF_BTF_LOAD)) return -EINVAL; @@ -5459,6 +5462,11 @@ static int bpf_btf_load(const union bpf_attr *attr, = bpfptr_t uattr, __u32 uattr_ if (attr->btf_flags & ~BPF_F_TOKEN_FD) return -EINVAL; =20 + bpf_attrs_init(&attrs, attr, uattr, uattr_size); + err =3D bpf_btf_load_log_attr_init(&log_attr, &attrs); + if (err) + return err; + if (attr->btf_flags & BPF_F_TOKEN_FD) { token =3D bpf_token_get_from_fd(attr->btf_token_fd); if (IS_ERR(token)) @@ -5476,7 +5484,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, &log_attr); } =20 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD fd_by_id_token_fd --=20 2.52.0 From nobody Mon Jan 26 22:44:37 2026 Received: from out-173.mta1.migadu.com (out-173.mta1.migadu.com [95.215.58.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 EC84133E35B for ; Mon, 26 Jan 2026 15:16:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769440569; cv=none; b=m5BkiUxJu/B1jc14L2f18lCJVPQeOavAgdMF8wSMhRKppjy3hzgViQYzU80rnxNxGmfh3hSQWaEIukY2Gv2ynx4Jo8x2YxJrOo1PcC+bRtIsXSvv6EYh4QYd4x1+/aH1TFgrPr45mHiC3Hzbfd5nexPJjJZ3RZFwMK7cW3qTcK4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769440569; c=relaxed/simple; bh=j7dw6UF6ug58KgBwYKbX0sX0RWaqzYAcnVLqOfVXLSc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YX7CYfkkAty0i/VJkVCB+whx8sWx5N1/8UC/dSez4u8xQX0Npz6x2tB8DcLJcBJE4oHnP9P3cWJ+2MZVM2aXYOgS9TTjasLQFinAHHFMpDu4J+27uB/f/RjWVa1zuPn4z0OFctuQPKejEQEnaswHBaLm/TQAHw+V/YwcyGSVEX4= 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=cY8aGCwK; arc=none smtp.client-ip=95.215.58.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="cY8aGCwK" 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=1769440563; 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=mHNwhGZ/a7bs2rBmc5MNX1NQzbdUO+5ibn0YYAlaad0=; b=cY8aGCwK2DtIqjbBpveG/aklLDp0XdpvQQKJGaQrXJG4vgRVWD3L4lm3RujVtL2e6ZP3QS x4kc5GzLpWnbgoij//0NJNSqzfEbbo4iLaj+db13Yojghw4UxFOMOVgI8j3sx5BjVWWfHb cWaPNOYJfc/qwAGrHkM89/4BdAy4xlY= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , Christian Brauner , Seth Forshee , Yuichiro Tsuji , Andrey Albershteyn , Leon Hwang , Willem de Bruijn , Jason Xing , Tao Chen , 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: [PATCH bpf-next v8 6/9] bpf: Add syscall common attributes support for btf_load Date: Mon, 26 Jan 2026 23:14:06 +0800 Message-ID: <20260126151409.52072-7-leon.hwang@linux.dev> In-Reply-To: <20260126151409.52072-1-leon.hwang@linux.dev> References: <20260126151409.52072-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" Since bpf_log_attr_init() now supports struct bpf_common_attr, pass the common attributes to it to enable syscall common attributes support for BPF_BTF_LOAD. Signed-off-by: Leon Hwang --- include/linux/bpf_verifier.h | 3 ++- kernel/bpf/log.c | 5 +++-- kernel/bpf/syscall.c | 8 +++++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index 28e22a03ac84..732bc4baee1c 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -642,7 +642,8 @@ struct bpf_log_attr { =20 int bpf_prog_load_log_attr_init(struct bpf_log_attr *log_attr, struct bpf_= attrs *attrs, struct bpf_attrs *attrs_common); -int bpf_btf_load_log_attr_init(struct bpf_log_attr *log_attr, struct bpf_a= ttrs *attrs); +int bpf_btf_load_log_attr_init(struct bpf_log_attr *log_attr, struct bpf_a= ttrs *attrs, + struct bpf_attrs *attrs_common); int bpf_log_attr_finalize(struct bpf_log_attr *log_attr, struct bpf_verifi= er_log *log); =20 #define BPF_MAX_SUBPROGS 256 diff --git a/kernel/bpf/log.c b/kernel/bpf/log.c index f1ed24157d71..3cccb0c5e482 100644 --- a/kernel/bpf/log.c +++ b/kernel/bpf/log.c @@ -902,13 +902,14 @@ int bpf_prog_load_log_attr_init(struct bpf_log_attr *= log_attr, struct bpf_attrs offsetof(union bpf_attr, log_true_size), attrs_common); } =20 -int bpf_btf_load_log_attr_init(struct bpf_log_attr *log_attr, struct bpf_a= ttrs *attrs) +int bpf_btf_load_log_attr_init(struct bpf_log_attr *log_attr, struct bpf_a= ttrs *attrs, + struct bpf_attrs *attrs_common) { const union bpf_attr *attr =3D attrs->attr; =20 return bpf_log_attr_init(log_attr, attrs, attr->btf_log_buf, attr->btf_lo= g_size, attr->btf_log_level, offsetof(union bpf_attr, btf_log_true_size), - NULL); + attrs_common); } =20 int bpf_log_attr_finalize(struct bpf_log_attr *log_attr, struct bpf_verifi= er_log *log) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 1ed007511776..040b105ab676 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -5449,7 +5449,8 @@ 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(const union bpf_attr *attr, bpfptr_t uattr, __u32 = uattr_size, + struct bpf_attrs *attrs_common) { struct bpf_token *token =3D NULL; struct bpf_log_attr log_attr; @@ -5463,7 +5464,7 @@ static int bpf_btf_load(const union bpf_attr *attr, b= pfptr_t uattr, __u32 uattr_ return -EINVAL; =20 bpf_attrs_init(&attrs, attr, uattr, uattr_size); - err =3D bpf_btf_load_log_attr_init(&log_attr, &attrs); + err =3D bpf_btf_load_log_attr_init(&log_attr, &attrs, attrs_common); if (err) return err; =20 @@ -6297,7 +6298,8 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr= , unsigned int size, err =3D bpf_raw_tracepoint_open(&attr); break; case BPF_BTF_LOAD: - err =3D bpf_btf_load(&attr, uattr, size); + bpf_attrs_init(&attrs_common, &attr_common, uattr_common, size_common); + err =3D bpf_btf_load(&attr, uattr, size, &attrs_common); break; case BPF_BTF_GET_FD_BY_ID: err =3D bpf_btf_get_fd_by_id(&attr); --=20 2.52.0 From nobody Mon Jan 26 22:44:37 2026 Received: from out-183.mta1.migadu.com (out-183.mta1.migadu.com [95.215.58.183]) (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 EC8523EBF21 for ; Mon, 26 Jan 2026 15:16:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.183 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769440575; cv=none; b=hreVknjuc6teje1CTV8Tl4VjDYBkQ4sPcCLJiD8UKpET1rthnSFJSgT0/QY2p8wLdoXiG2XHKLjLcZz5vJgzpA2Fz2os3vChKNTBYxp7UjDW9Y9mwA8RUb8jGQwf7dbK+jxUs4/VEf+65kAbHd9YbMhvwvPm865mgL9t6D9dj58= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769440575; c=relaxed/simple; bh=jFbFzaLQVKRbg8pqCam/KLxE2rR+AZ8YnXVgAtjdnJA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aWHhfN9PxHy2g4cVlHJZb425H2Ls/2Ve9bfLfXsiQpYWCrt4SKo6Kb+GmN45TSlYTznIHO/Qzr+G7nSCxZn58+24I9d9H39iDAohF76ZoGzdm7PN79M7h224BwBJm88Po+7EHlE4Ocwzi1q9Csv2qAiTBXGNILxR7VPopw45Xzk= 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=GiLZ5Hcv; arc=none smtp.client-ip=95.215.58.183 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="GiLZ5Hcv" 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=1769440571; 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=nJlY7pNuViDwTIygCdfwSue8vGZGp+4ZNyXH17+V1/E=; b=GiLZ5HcvR3XxMi+Vws3yhVQDdkQKkF7rZah6msxuCZzdPojhGNYczEygw08S5DPSHMLjVF pQdjuPbPG7TKjNLCZ50VCpY/B7rN66mwtsHnZX55P9Php6tfnM2SJrFwYHp30M/QsmwOrr 6Tpv5yaC45+b7SRkg7bVKvUxno5T9Gk= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , Christian Brauner , Seth Forshee , Yuichiro Tsuji , Andrey Albershteyn , Leon Hwang , Willem de Bruijn , Jason Xing , Tao Chen , 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: [PATCH bpf-next v8 7/9] bpf: Add syscall common attributes support for map_create Date: Mon, 26 Jan 2026 23:14:07 +0800 Message-ID: <20260126151409.52072-8-leon.hwang@linux.dev> In-Reply-To: <20260126151409.52072-1-leon.hwang@linux.dev> References: <20260126151409.52072-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 userspace. With extended BPF syscall support, detailed error messages can now be reported via the log buffer, allowing users to understand the specific reason for a failed map creation. Signed-off-by: Leon Hwang --- include/linux/bpf_verifier.h | 2 ++ kernel/bpf/log.c | 30 +++++++++++++++++ kernel/bpf/syscall.c | 65 ++++++++++++++++++++++++++++++------ 3 files changed, 87 insertions(+), 10 deletions(-) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index 732bc4baee1c..917293a552b6 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -644,6 +644,8 @@ int bpf_prog_load_log_attr_init(struct bpf_log_attr *lo= g_attr, struct bpf_attrs struct bpf_attrs *attrs_common); int bpf_btf_load_log_attr_init(struct bpf_log_attr *log_attr, struct bpf_a= ttrs *attrs, struct bpf_attrs *attrs_common); +struct bpf_verifier_log *bpf_log_attr_create_vlog(struct bpf_log_attr *log= _attr, + struct bpf_attrs *attrs_common); int bpf_log_attr_finalize(struct bpf_log_attr *log_attr, struct bpf_verifi= er_log *log); =20 #define BPF_MAX_SUBPROGS 256 diff --git a/kernel/bpf/log.c b/kernel/bpf/log.c index 3cccb0c5e482..d7933a412c36 100644 --- a/kernel/bpf/log.c +++ b/kernel/bpf/log.c @@ -912,6 +912,36 @@ int bpf_btf_load_log_attr_init(struct bpf_log_attr *lo= g_attr, struct bpf_attrs * attrs_common); } =20 +struct bpf_verifier_log *bpf_log_attr_create_vlog(struct bpf_log_attr *log= _attr, + struct bpf_attrs *attrs_common) +{ + const struct bpf_common_attr *common =3D attrs_common->attr; + struct bpf_verifier_log *log; + int err; + + memset(log_attr, 0, sizeof(*log_attr)); + log_attr->log_buf =3D common->log_buf; + log_attr->log_size =3D common->log_size; + log_attr->log_level =3D common->log_level; + log_attr->attrs_common =3D attrs_common; + + if (!log_attr->log_buf) + return NULL; + + log =3D kzalloc(sizeof(*log), GFP_KERNEL); + if (!log) + return ERR_PTR(-ENOMEM); + + err =3D bpf_vlog_init(log, log_attr->log_level, u64_to_user_ptr(log_attr-= >log_buf), + log_attr->log_size); + if (err) { + kfree(log); + return ERR_PTR(err); + } + + return log; +} + int bpf_log_attr_finalize(struct bpf_log_attr *log_attr, struct bpf_verifi= er_log *log) { u32 log_true_size, off; diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 040b105ab676..a596a3f22ade 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -1370,7 +1370,7 @@ static bool bpf_net_capable(void) =20 #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_v= erifier_log *log) { const struct bpf_map_ops *ops; struct bpf_token *token =3D NULL; @@ -1382,8 +1382,10 @@ static int map_create(union bpf_attr *attr, bpfptr_t= uattr) int err; =20 err =3D CHECK_ATTR(BPF_MAP_CREATE); - if (err) + if (err) { + bpf_log(log, "Invalid attr.\n"); return -EINVAL; + } =20 /* 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 @@ -1392,17 +1394,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) @@ -1410,13 +1420,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) @@ -1434,8 +1448,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 @@ -1518,8 +1534,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); @@ -1542,6 +1560,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; } @@ -1569,6 +1588,7 @@ static int map_create(union bpf_attr *attr, bpfptr_t = uattr) bpfptr_t uprog_hash =3D make_bpfptr(attr->excl_prog_hash, uattr.is_kerne= l); =20 if (attr->excl_prog_hash_size !=3D SHA256_DIGEST_SIZE) { + bpf_log(log, "Invalid excl_prog_hash_size.\n"); err =3D -EINVAL; goto free_map; } @@ -1584,6 +1604,7 @@ static int map_create(union bpf_attr *attr, bpfptr_t = uattr) goto free_map; } } else if (attr->excl_prog_hash_size) { + bpf_log(log, "Invalid excl_prog_hash_size.\n"); err =3D -EINVAL; goto free_map; } @@ -1622,6 +1643,29 @@ static int map_create(union bpf_attr *attr, bpfptr_t= uattr) return err; } =20 +static int map_create(union bpf_attr *attr, bpfptr_t uattr, struct bpf_att= rs *attrs_common) +{ + struct bpf_verifier_log *log; + struct bpf_log_attr log_attr; + int err, ret; + + log =3D bpf_log_attr_create_vlog(&log_attr, attrs_common); + if (IS_ERR(log)) + return PTR_ERR(log); + + err =3D __map_create(attr, uattr, log); + if (err >=3D 0) + goto free; + + ret =3D bpf_log_attr_finalize(&log_attr, log); + if (ret) + err =3D ret; + +free: + kfree(log); + return err; +} + void bpf_map_inc(struct bpf_map *map) { atomic64_inc(&map->refcnt); @@ -6234,7 +6278,8 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr= , unsigned int size, =20 switch (cmd) { case BPF_MAP_CREATE: - err =3D map_create(&attr, uattr); + bpf_attrs_init(&attrs_common, &attr_common, uattr_common, size_common); + err =3D map_create(&attr, uattr, &attrs_common); break; case BPF_MAP_LOOKUP_ELEM: err =3D map_lookup_elem(&attr); --=20 2.52.0 From nobody Mon Jan 26 22:44:37 2026 Received: from out-187.mta1.migadu.com (out-187.mta1.migadu.com [95.215.58.187]) (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 A19E133C1A5 for ; Mon, 26 Jan 2026 15:16:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.187 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769440582; cv=none; b=DpMGx81LcmFdRvgI9km+T56+ab7K7mH98kv9tU4lVHjwJ7koMVLsAzVPULotN05qGFNdpij1tDKT5rSUDquswJhjSh7f9fpRpYSCc2Wnojk8VZjJ/nHCWqzM/zrmOi9TXKkoCqMkKhuGO/fNS6YRT0sh5juK+yUqqxAEJShJaxI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769440582; c=relaxed/simple; bh=RoVqq0qKRTtZkvPyA0OK31cvnMmOvon7veF/oreYESs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QSR7D3SPhhg5N4nqnximd9BfqbHn2Ed4IjqdjCoHoMV/4iwy+PNPDhfIKw8XIpyqYOQkGN8PketnzFQTNWmZqYaLI5tywSS0cl+8uskQ1PiPXpVbQB9vkt85U52PRcHUwTFvOyYAnxdz7YaOIUM2eCWHZ+w+AlK8S8Be64u8duI= 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=XY2LFlMy; arc=none smtp.client-ip=95.215.58.187 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="XY2LFlMy" 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=1769440578; 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=AoKtZ5+P7Wxb1aJQ0fxakYbtUswnhN9EllnMlBVZL/I=; b=XY2LFlMyRua21zVYPVq55onF/qOGA8JQjbtcDl33ZQaykqMaOzcBXT0m+MAK9yp+7uvbeJ DA0mN/Nb63QQ2c25Jn4ka4mKGU9jPegmuhN65c64aH0wvqMolVVLzJq78OXHHr7C2TkIuB 5rVkLDoUK1joaJ1Cuq9Um9/ZH9ZP6oc= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , Christian Brauner , Seth Forshee , Yuichiro Tsuji , Andrey Albershteyn , Leon Hwang , Willem de Bruijn , Jason Xing , Tao Chen , 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: [PATCH bpf-next v8 8/9] libbpf: Add common attr support for map_create Date: Mon, 26 Jan 2026 23:14:08 +0800 Message-ID: <20260126151409.52072-9-leon.hwang@linux.dev> In-Reply-To: <20260126151409.52072-1-leon.hwang@linux.dev> References: <20260126151409.52072-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, users can now retrieve detailed error messages when map creation fails via the log_buf field. Introduce struct bpf_log_opts with the following fields: log_buf, log_size, log_level, and log_true_size. Extend bpf_map_create_opts with a new field log_opts, allowing users to capture and inspect log messages on map creation failures. Signed-off-by: Leon Hwang --- tools/lib/bpf/bpf.c | 16 +++++++++++++++- tools/lib/bpf/bpf.h | 17 ++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index 9d8740761b7a..0c3e40844d80 100644 --- a/tools/lib/bpf/bpf.c +++ b/tools/lib/bpf/bpf.c @@ -209,6 +209,9 @@ int bpf_map_create(enum bpf_map_type map_type, const struct bpf_map_create_opts *opts) { const size_t attr_sz =3D offsetofend(union bpf_attr, excl_prog_hash_size); + const size_t attr_common_sz =3D sizeof(struct bpf_common_attr); + struct bpf_common_attr attr_common; + struct bpf_log_opts *log_opts; union bpf_attr attr; int fd; =20 @@ -242,7 +245,18 @@ 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); + log_opts =3D OPTS_GET(opts, log_opts, NULL); + if (log_opts && feat_supported(NULL, FEAT_BPF_SYSCALL_COMMON_ATTRS)) { + memset(&attr_common, 0, attr_common_sz); + attr_common.log_buf =3D ptr_to_u64(OPTS_GET(log_opts, log_buf, NULL)); + attr_common.log_size =3D OPTS_GET(log_opts, log_size, 0); + attr_common.log_level =3D OPTS_GET(log_opts, log_level, 0); + fd =3D sys_bpf_ext_fd(BPF_MAP_CREATE, &attr, attr_sz, &attr_common, attr= _common_sz); + OPTS_SET(log_opts, log_true_size, attr_common.log_true_size); + } else { + fd =3D sys_bpf_fd(BPF_MAP_CREATE, &attr, attr_sz); + OPTS_SET(log_opts, log_true_size, 0); + } return libbpf_err_errno(fd); } =20 diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h index 2c8e88ddb674..59673f094f86 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_log_opts { + size_t sz; /* size of this struct for forward/backward compatibility */ + + char *log_buf; + __u32 log_size; + __u32 log_level; + __u32 log_true_size; + + size_t :0; +}; +#define bpf_log_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_log_opts *log_opts; + size_t :0; }; -#define bpf_map_create_opts__last_field excl_prog_hash_size +#define bpf_map_create_opts__last_field log_opts =20 LIBBPF_API int bpf_map_create(enum bpf_map_type map_type, const char *map_name, --=20 2.52.0 From nobody Mon Jan 26 22:44:37 2026 Received: from out-181.mta0.migadu.com (out-181.mta0.migadu.com [91.218.175.181]) (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 9A68633E371; Mon, 26 Jan 2026 15:16:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769440606; cv=none; b=pSUfNqKl0HKT7wR/c4eXPWwDLRbngRczM7K2VnDjzGU44toWFt0nbrmF8KYuiB37RyfUGctk/pDbo8Tj7CQyFupRVLP9gTRHizurHvp+4h5dtJi1jDbZoNRvHzdQsXpCvU4RqjnhmZW5lPPHGWY+uFttTRQV/xPPbQJWJHaWvxU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769440606; c=relaxed/simple; bh=lRMaXBCmuroYEA8EMWnl7dKsDl+uPc5spkCIVT8MdKg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Aq5rlv+8FM+sgxgI8hbPAYJVgzm3eMOUum2LSZ7/s/UJCWl/V6I0AwozD8leQx99rmGdJmoPTazeehJMlFVt7eMMjCNbMYqhj2ZXQ5MP06ai2rrOx8iySGlpIbCKPF6eGxI4qTbL+suBU35El24sjqsOyNIWIAAuEVdyCUGYSZw= 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=UGIom/7x; arc=none smtp.client-ip=91.218.175.181 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="UGIom/7x" 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=1769440599; 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=bTUbEhqHJLhG8hIlAVdEqIlik/ffHI3gpe+iOi5xJpk=; b=UGIom/7xXk6iuFieLFg0kOOqCgjiSBRXqujSmBg/PrLQFX5hIX6UOOWOhmrm1NkyAdDsWG cJFPuOdmzyP/8Utv/2YsmHkiAW+dXPjr1+9Q4HVEbn0oJOZ2iV5q/j0ltzoBOmnZdb54qg bfLOXnx2muqc8fiptwdrts+y+29rIKg= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , Christian Brauner , Seth Forshee , Yuichiro Tsuji , Andrey Albershteyn , Leon Hwang , Willem de Bruijn , Jason Xing , Tao Chen , 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: [PATCH bpf-next v8 9/9] selftests/bpf: Add tests to verify map create failure log Date: Mon, 26 Jan 2026 23:14:09 +0800 Message-ID: <20260126151409.52072-10-leon.hwang@linux.dev> In-Reply-To: <20260126151409.52072-1-leon.hwang@linux.dev> References: <20260126151409.52072-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" Add tests to verify that the kernel reports the expected error messages when map creation fails. Signed-off-by: Leon Hwang --- .../selftests/bpf/prog_tests/map_init.c | 168 ++++++++++++++++++ 1 file changed, 168 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..89e6daf2fcfd 100644 --- a/tools/testing/selftests/bpf/prog_tests/map_init.c +++ b/tools/testing/selftests/bpf/prog_tests/map_init.c @@ -212,3 +212,171 @@ 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_log_opts, log_opts); + + log_buf[0] =3D '\0'; + log_opts.log_buf =3D log_buf; + log_opts.log_size =3D sizeof(log_buf); + log_opts.log_level =3D BPF_LOG_FIXED; + opts->log_opts =3D &log_opts; + 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(log_opts.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); +} + +static void test_excl_prog_hash_size_1(void) +{ + const char *msg =3D "Invalid excl_prog_hash_size.\n"; + const char *hash =3D "DEADCODE"; + LIBBPF_OPTS(bpf_map_create_opts, opts, + .excl_prog_hash =3D hash, + ); + + test_map_create_array(&opts, msg); +} + +static void test_excl_prog_hash_size_2(void) +{ + const char *msg =3D "Invalid excl_prog_hash_size.\n"; + LIBBPF_OPTS(bpf_map_create_opts, opts, + .excl_prog_hash_size =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(); + if (test__start_subtest("invalid_excl_prog_hash_size_1")) + test_excl_prog_hash_size_1(); + if (test__start_subtest("invalid_excl_prog_hash_size_2")) + test_excl_prog_hash_size_2(); +} --=20 2.52.0