From nobody Sat Feb 7 17:55:22 2026 Received: from out-179.mta0.migadu.com (out-179.mta0.migadu.com [91.218.175.179]) (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 8F66B23957D; Fri, 23 Jan 2026 03:25:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769138756; cv=none; b=IP4OBT00J8690j+feaFL5N8raMD5LxwyzKqrn5KDrAS9IzHuSVPmmqm1srdgW5QxD1M7S1zNsq6Dsor9bsvw8et2qnDHJAmKxHG1VERl+PiDVW82H9d3xAWzOwoq7u73BNqZD37vWdXHJu9zRky4FQy7j+yqUuQd42XNhBdsse4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769138756; c=relaxed/simple; bh=Vgg+A0HbBP3zzBVE4DqTFe6MGreP/T+yPlzVnIkw0f0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lDmDKE3J2e1yR31rGrcmRCWT1c5lHmFVWD2S0HghBaGwaae/njSFM0pPSH1K+aBsQeMBPaep+BiNawuSJPRHPemDfklI7GjSsNFoiztckGyu8dKvKWCW2M0v3p4NWWN1pQSRFrevIuqmxBy8m4AhSzaEu1PT67gGu/GNIM+b5Y4= 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=Vhu9vWus; arc=none smtp.client-ip=91.218.175.179 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="Vhu9vWus" 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=1769138741; 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=sxYCK5TeXJjlRyHC8Xd7OAEQILqAVW/X/B15MOR30rM=; b=Vhu9vWuslMW7+IZG1lMQ0J8RG3FWMvY/EiG9fa3DCmwsTDzXlzCKyuLLQnZgFLAfF4lFkN nZ+AsQh+Ksfqotq8atNJ/yr01ozZtPQWlR27NbUqoTzKOQR6gtmo4nm2h6giuc72Ae7/ms ZtfkOflfGEtMLPUK+xV55yoR0NEN26k= 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 v7 1/9] bpf: Extend BPF syscall with common attributes support Date: Fri, 23 Jan 2026 11:24:37 +0800 Message-ID: <20260123032445.125259-2-leon.hwang@linux.dev> In-Reply-To: <20260123032445.125259-1-leon.hwang@linux.dev> References: <20260123032445.125259-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 2a2ade4be60f..814bd2debd5b 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 { @@ -1491,6 +1492,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 3c5c03d43f5f..3564b5bf3689 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -6163,8 +6163,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 @@ -6178,6 +6180,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; @@ -6313,9 +6329,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, @@ -6346,7 +6363,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 b816bc53d2e1..c14f9c6a275c 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 { @@ -1491,6 +1492,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:22 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 F2C6437E315 for ; Fri, 23 Jan 2026 03:25:52 +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=1769138768; cv=none; b=rs8le0BhzKtbDWwhTngzTag40KzjweHNIaOCGjyj9t9eYc3vTcxib39boTOLkhLDHbnWYkx6/9N68K4v1FuGrhXmdwLubYcHV5Qih8cc27XysD6dSVR88zkxRZ3Cxe8pY1BfmZws/Nes9LcNza5vD1a9mXuh+PDGYLYwq0KjJdE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769138768; c=relaxed/simple; bh=no3ObbSyvuuqZyex6syDjz8+bFeyWX4CAg9DVBnfq/E=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MupKZwmGbuuiFcEwMzGq2ciTQq7E/C6fz9T6+vznPtAy8GzMw+Wsa3ayzZu3HWtCtQ+ReW6z25zkw8h7BZ53W4r0jiNqDeRGVJf+/2EGLhVPl6gHJJiWWlO5D9mpjDekXv/y6PqAHkqdmMHUYHTDbsvOO6M0hnA2MHhO27kePBM= 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=CNvbOZog; 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="CNvbOZog" 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=1769138748; 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=AtQ4FEwwZrYOozDI7ZiBSHyz4si41HRxpth08zPHU2A=; b=CNvbOZogCbPyWVUNsm0NrK+PRaxT0UnyfJUcUPDKleNv+imLoYta7c6NoQ5nohGTMOpM6o ZJpYAMezJgpojBMWKG5T0wqsgOg5oGxjJkahivdWb8KAtCVJ2X23gVSYIsgVdfsM3p47yn 6M4Lodvby4F5GzEYOTkhyX73MZoE+I4= 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 v7 2/9] libbpf: Add support for extended bpf syscall Date: Fri, 23 Jan 2026 11:24:38 +0800 Message-ID: <20260123032445.125259-3-leon.hwang@linux.dev> In-Reply-To: <20260123032445.125259-1-leon.hwang@linux.dev> References: <20260123032445.125259-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 | 11 ++++++++++ tools/lib/bpf/libbpf_internal.h | 3 +++ 3 files changed, 50 insertions(+) diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index 21b57a629916..fc87552b1378 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 : -errno; +} + 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..e82c2afead43 100644 --- a/tools/lib/bpf/features.c +++ b/tools/lib/bpf/features.c @@ -506,6 +506,14 @@ 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) +{ + int ret; + + ret =3D probe_sys_bpf_ext(); + return ret > 0; +} + typedef int (*feature_probe_fn)(int /* token_fd */); =20 static struct kern_feature_cache feature_cache; @@ -581,6 +589,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 Sat Feb 7 17:55:22 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 2781627603F for ; Fri, 23 Jan 2026 03:26:15 +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=1769138785; cv=none; b=W3hdJXLqjbbFpLDCoOAc9DB7IWa/+ameoCRfEZ7Zxiat2xBaWK6D4x3h6znhlQ0t7YdM5959BjpRYxsH0/27Voq76NevdyKD41k3Zj03vSzZ5e5ygZrNUqndqUnRsKj3Ruzr+HpcCFS2jdZxg8kK0KZYY+iJxJ4jysLXEvwbUjg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769138785; c=relaxed/simple; bh=D8VzSPttr4Hr5rWQCBVo/JsjJYyU/BRovQ+du6UErK0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aXzbgoR6R9GRQPmTLPA/4YwSg+n6bQjhMwHOQUXDx2Kx3p4SmhvWEgnHaj+z6+LiusfQ5C1NV8AT12YmMm8gFvXeou9vKomxCoy6G7UhBpjouc7CHXah/A08cfY2jDooZDhbzUXFodAaiPoqTiAzpr2qtA1OidxJ//5aHPYXJyw= 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=VapBBXIa; 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="VapBBXIa" 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=1769138771; 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=yJFWnLixWM26F/goIhXEZzpWInfDRjLykyliX7jQH+k=; b=VapBBXIa8HjXzbgLG2yt5RXYtg02hVQ7cBnleqVboMAb6MEHidZ9rgm1CnJFD7GDxHhm5R lHw2K3KlwWCXcIfgC+pyYSb/eNDAfSuf3fBWXYY2FVFAlZI5OErjweewH1qJQJYQxhpGr9 kOrwrhGxX/MSINy0G5dLQVNEP2IJQz4= 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 v7 3/9] bpf: Refactor reporting log_true_size for prog_load Date: Fri, 23 Jan 2026 11:24:39 +0800 Message-ID: <20260123032445.125259-4-leon.hwang@linux.dev> In-Reply-To: <20260123032445.125259-1-leon.hwang@linux.dev> References: <20260123032445.125259-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 5936f8e2996f..3a525a7e8747 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -2867,8 +2867,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 3564b5bf3689..8468baf545c5 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 c7f5234d5fd2..03d56d3d3f89 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -25593,12 +25593,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); @@ -25645,9 +25645,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 @@ -25797,17 +25797,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 Sat Feb 7 17:55:22 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 58EDC34D4FB for ; Fri, 23 Jan 2026 03:26:24 +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=1769138802; cv=none; b=QAA/nAz58HJ89YiL2UgaP0nd3ba7cQ2pLz8L09EUrjdvvII0sHgwKy8z/gARgq3PFxSkuMisPlr2F2EZyiHUcQbDJc/+9Eo+wCqyhyupXxcpZ1hbdP1urQIy2dIQxMIsyLdjWuh7pjJo+vIMaXaCD7aNs7tQI2CvNC1YJBzBJao= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769138802; c=relaxed/simple; bh=USSgFhIgNkJkCa3MhLwDzVL9wdU3Z47qDGqjnKZ+GeQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QeC0IfkPCTFsX6ZyjwQSTKX/8lal6c9PNhfIIzBM5HAQzHq3kCwfp4bRCkVDYw5BvwJZuuxlWReOHMp5jwQEgJ8+lMlKkuA55fmxKjK2JsxoDzhQurYRwoTFEql8tA4cAQcxpOKGTLF5Q13PsYnpq1i6pBrMXUklIxj/esXKxQw= 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=LJ0jBzcU; 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="LJ0jBzcU" 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=1769138778; 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=4oNysg7eRr3NvRmo80BWS1H7XtTEokr9/t7DGX+DMqc=; b=LJ0jBzcUnbDsWuiYeK/Gw+Xw3UmsmU3sYL4we9sMddwhse2YXKlcUGwxnm3joRkgHwCAGL lXDNc4NrFFGC+i7BL/fIND9yt8I7KhNWIRR92gq+9ke8UbYFb5P9x9jdxmPK0BxKuL8n0S 52SY1ouZtdJ/UQ09ivF0M21UOD/2YcA= 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 v7 4/9] bpf: Add syscall common attributes support for prog_load Date: Fri, 23 Jan 2026 11:24:40 +0800 Message-ID: <20260123032445.125259-5-leon.hwang@linux.dev> In-Reply-To: <20260123032445.125259-1-leon.hwang@linux.dev> References: <20260123032445.125259-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 8468baf545c5..8f3e489667d7 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 @@ -6174,6 +6175,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 @@ -6225,7 +6227,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 Sat Feb 7 17:55:22 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 EB12C30F921 for ; Fri, 23 Jan 2026 03:26:31 +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=1769138798; cv=none; b=OdvrDGtDk2r1O9d3pTRtqL/CbZfQbWCVWPyTrvgbD+jrmCvTiD+vDCsN19XdpPeODldX9kpeQkYoeGAgF1O/TCpPpWg1WRDC6V9H6k9rY+Kn5bQGc0hOeky+HBL07OuYw5kOuoRqMqcUrhP61GEnaCE5lzFCjSHFknGenjDzRV4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769138798; c=relaxed/simple; bh=3CDRb5TfBaaU8wObRpIAxISLoRybRiGI7jGZei7pTIw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XBHwzq4xBIJsR1tYvK9jByXPb/EYPswQdfyzyMZKILTAE/rIPcXvRw+mhfxXjDVIMBh7Cp5MBBGgDCLUyGp8anFgE34tSDgJCye1vElxIE0ZNdnwDYmikdl5eudIAjrs1RJcsjwndSjHusndLw7DEcAlr955cKmaUeILYSL67ho= 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=xakdCuCo; 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="xakdCuCo" 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=1769138786; 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=v1mZt6DIO7MMOA55DtyJvBYGEgLMXpTZ3zAejg5xJ4o=; b=xakdCuCoh+5akxxwDX4cYCZVZsOHGMXt6/i1V6VGA7QDUGQCwaUvBHBM9P1eNQUEcSnD68 v/L0+t8PRD7GD9dTFKvXWfCeOndme5Ww9fjqilAHyJWJWrHEMHMvLFqGvz0tMMOZhdj0gA qGrGWGQ5ixmHz5WbWMbcXva1g8YnsXQ= 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 v7 5/9] bpf: Refactor reporting btf_log_true_size for btf_load Date: Fri, 23 Jan 2026 11:24:41 +0800 Message-ID: <20260123032445.125259-6-leon.hwang@linux.dev> In-Reply-To: <20260123032445.125259-1-leon.hwang@linux.dev> References: <20260123032445.125259-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 d10b3404260f..136fdd8f73b2 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: @@ -8134,12 +8120,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 8f3e489667d7..6241ef35c164 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -5436,6 +5436,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; @@ -5443,6 +5446,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)) @@ -5460,7 +5468,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 Sat Feb 7 17:55:22 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 9CA3B381718 for ; Fri, 23 Jan 2026 03:27:01 +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=1769138828; cv=none; b=V0q2gD2H/6L8rL9iY5ibESJ4lbxqXJM6+sal0ndJIzbiN5pjo7/K0b2em5VYvUv6BUb4jGkinoCIq2RuswM8zNc4GSkHpuQxh66m0ayyIRwzGw3/ooMDjYI+smdryyTuNH2l/WtAgPA2FgkcF6LTJiQJy8pHEVWKv6v+QhDmO6U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769138828; c=relaxed/simple; bh=y1kKlElWA1x/XM6Wz5R/d/glsIS6y2hLv2QEDt32Fc8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fghGjGI3jYJ0ZKL/0ju3ihdCrp7x0uljbKOOQnDhxOX+la4qksTO2NuVxe2RW3f0BjP/2+klKvbwzqCra6IA2gpe3oV/SinuOJvxlXRpoACp+7jNm7uZk5X/X5asCCnzkMEe5CCkftpx4ZIpes7dL4qJDn8Fi8h8BlDtAcDNhf4= 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=owjQ6Zry; 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="owjQ6Zry" 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=1769138818; 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=lUdsbdc1S8X/9qw1zh3i4z0z65VGYSHUbuVLsq9rNHU=; b=owjQ6Zrywu1+UzaCdloCz6uSqOV57ZHTYsNFI8iqdNGRP0NSFrmyRjmDb7cdOmE6fBzrd4 xQM7J3rSeTs09UDnRnwsdFiOyS1Fn3DPTkVIdlY4GrTW7HUGTP0ksgMsFiv4iIOJkXuWz6 sWbOn6Z/s4NNvsoYj9g0vfz5fdogG7o= 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 v7 6/9] bpf: Add syscall common attributes support for btf_load Date: Fri, 23 Jan 2026 11:24:42 +0800 Message-ID: <20260123032445.125259-7-leon.hwang@linux.dev> In-Reply-To: <20260123032445.125259-1-leon.hwang@linux.dev> References: <20260123032445.125259-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 6241ef35c164..d17cb243c5b3 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -5433,7 +5433,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; @@ -5447,7 +5448,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 @@ -6281,7 +6282,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 Sat Feb 7 17:55:22 2026 Received: from out-171.mta1.migadu.com (out-171.mta1.migadu.com [95.215.58.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 D0CFE23184A; Fri, 23 Jan 2026 03:27:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769138838; cv=none; b=ZNmSYZY2fdeNBCrcMiz/a5gY9uwCxrNRhesmE3Y1ho1zg1KubV4JINZjB5S9+cFUlgcf6CDkfYB4jCZ2fukz0VAM1AzwE6uKF5GuruPn9nxo/EORJrz/zABUsHpd3qwnLI8w7lFv/N/TO3MO1DSd9R5S4JShSzewpTB1oAQO3Rk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769138838; c=relaxed/simple; bh=xDZwIhdRkH9HMsTxXrADglJbJvAqEi6uuzp9cLjaFcM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=N6s7OG4ztYXFmYkq+45asAodEuEM1lDATRxYgbPiNwEvUpnxJzKRK8ShyFtpszs+jGgbU5VEhlMqrjQulGVpje+037GRdaplJ5A/7GrBKINWqastogxU9P2zkKCERI2l7WW+D8DAiA+hxMY6efFGeotONBrFF1pf+62Uj11CtVw= 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=aEmizzxr; arc=none smtp.client-ip=95.215.58.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="aEmizzxr" 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=1769138823; 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=w20SWqadRIZvVxbk6ffDVL8xOLFvzsfnBYd24iyVVkg=; b=aEmizzxrAWdh2hPGUVfwPkwYcECTQr/DccD+Ddvajl2w3VhVQQBlCIOl4n4Lp4QAWFwVYy IFLDynGHThftPRcBpFAqlYlEYRraDoF//Pzr95s+9954JdvVcucjLKFlpqEJVCQmVyF/Yl GolqCfdzAzViNPTSkkLEEqKdkwrSHAw= 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 v7 7/9] bpf: Add syscall common attributes support for map_create Date: Fri, 23 Jan 2026 11:24:43 +0800 Message-ID: <20260123032445.125259-8-leon.hwang@linux.dev> In-Reply-To: <20260123032445.125259-1-leon.hwang@linux.dev> References: <20260123032445.125259-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 d17cb243c5b3..0cfa4029a829 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); @@ -6218,7 +6262,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 Sat Feb 7 17:55:22 2026 Received: from out-189.mta1.migadu.com (out-189.mta1.migadu.com [95.215.58.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 8D15E389DEB for ; Fri, 23 Jan 2026 03:27:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.189 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769138845; cv=none; b=QuMxGPbB3pWA0SmibgqYY4I/8x+CQCvBdRVgY6wOA8pROzsRsm/7GEAaj5pgDlpe1t1MxSnaanAdN6jrjpk068G4TKgOPNbxKBTBrlebVLWy3uxH2oc2b3Vzj5YsRPcaBCDDU4pPXomOuzxmlRsR69lMxSApmpNxt8fq3SXzPMg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769138845; c=relaxed/simple; bh=T18ATinwQYYomVz8gfwoZUmH3Mot02ptEZTGQaJMhNc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=d8nqLMhhSDxap5M8HodTqEx+m0s6/ajlqWPMVUBzstl6Y7CSERUGwiuoSDIOr79OgZD01dvwKmOGa+4ozk+HDXLrGpOv0WrP7Biyhgy9rjtnDa15NqEg15ZzDLTKd/w8N8Ia98a6d+98KkCI8pZs+SG4BGxepgqPQi3GmRmVH0M= 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=Ra16I90Q; arc=none smtp.client-ip=95.215.58.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="Ra16I90Q" 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=1769138830; 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=g05SE4IaZYfhAoFlpmIzKfvmJDcsynvYyH/TG1loacs=; b=Ra16I90QGhCY9ZMvFmqomQa+dHWVpoGZVnNEHos0nkwWRYRuaDfulRNphRYrWFGixoAJE3 hG5hu5lCQ1nVYm70jDWs1sPtksUNiWU6MIZfR+5FMH/VHIiaVUXSZenSf0MdNsZJwBxPV6 nYp1ZBMp5OMi3vKTaC0pqzRfLygN74U= 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 v7 8/9] libbpf: Add common attr support for map_create Date: Fri, 23 Jan 2026 11:24:44 +0800 Message-ID: <20260123032445.125259-9-leon.hwang@linux.dev> In-Reply-To: <20260123032445.125259-1-leon.hwang@linux.dev> References: <20260123032445.125259-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 fc87552b1378..eb9127af6d04 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 Sat Feb 7 17:55:22 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 2D0CF37AA64 for ; Fri, 23 Jan 2026 03:27:58 +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=1769138882; cv=none; b=SQ/YsvGe9245eLS6XS1JNuEP8qzDHjH++fPIYgVudyikFQnhDbJlPYVyQpaKxjUoyzEVfRkiIqRDUSXSgFbz35GXnU9igYZ2XlxROlirT13le83XJ0SasKeVVNTbXmeQH2zBo8l1BWBCDWBDiJYHQvqoLtIzLDd/1gWuY9Cn0I0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769138882; c=relaxed/simple; bh=lRMaXBCmuroYEA8EMWnl7dKsDl+uPc5spkCIVT8MdKg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eAQQUoAltMScjVEW/GBrnxWdJhNh2Gt1xkY2xml1gmmup+wd3vpiEVOeTKvdIRiG30+FMv4pM30b5csNlsE2pSvZ3W+Hyz+pPh8y+s7wn0xN+yPj7oq49FRLJ0uR9CJIAob5gb81Cvz+EKKxmaXfJAfKlKmuUWkQhw/dIy3/FuQ= 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=p3de64NB; 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="p3de64NB" 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=1769138874; 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=p3de64NBo2GuBRMd8KI4UxCOh+2YfnvYNe1smjLLhMzjySQVECbxFRrEvkT/Ivm7RgT6h7 rJYLy3Tiw28Ui7bSSWKouw7Fz/tgLDxraDifeneDUWFjrdrVZEfvAORXKsBaqra2j1Z7KK mnWNIGhFVKfKsCAeoDd9PTgrn921kUQ= 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 v7 9/9] selftests/bpf: Add tests to verify map create failure log Date: Fri, 23 Jan 2026 11:24:45 +0800 Message-ID: <20260123032445.125259-10-leon.hwang@linux.dev> In-Reply-To: <20260123032445.125259-1-leon.hwang@linux.dev> References: <20260123032445.125259-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