From nobody Tue Jan 27 00:09:33 2026 Received: from out-173.mta0.migadu.com (out-173.mta0.migadu.com [91.218.175.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 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