From nobody Mon Feb 9 08:53:45 2026 Received: from mail-pj1-f66.google.com (mail-pj1-f66.google.com [209.85.216.66]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1DAAB14EC73 for ; Sat, 24 Jan 2026 06:20:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.66 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769235659; cv=none; b=dWObxSKvqgB9I1bEqyxSseLXqrVGQJPEIB9XNZP2JADlp42Y+cZhxUdQpCoro05KYSyD1/2ifsVVX+llVYQvw5j4wv70y+Zbl0aUmx9jzNfA3Ql/7VxS2Sx9kuO+M8zuFD7VVzDUrSL+zB90FE2Jc+Px9NLGyrq5Tkmt+LBPwHY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769235659; c=relaxed/simple; bh=K9b/t9XXOnTLdqEXsxRrneyFY/nndVnsOJwQPHo9UMg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iq3P4Qaog6a06dN3/FcBj0bukggjxFFJ4DFCG4ibQ5EUa6sfKYH/XokLa39d0lGoFHtUjGHZshs39nNXD/+WXfQ27ERoT8uPVGy5cIC37eOb+lYkuIX2J/f0k1NS5qh/yUDYq0E4KsD2KO6TN5z07bbtZ/6+75KzHdiIrKzC2co= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=gpUgRNGu; arc=none smtp.client-ip=209.85.216.66 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="gpUgRNGu" Received: by mail-pj1-f66.google.com with SMTP id 98e67ed59e1d1-352ccc61658so1339067a91.0 for ; Fri, 23 Jan 2026 22:20:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769235657; x=1769840457; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=uaR31gnegNovKwQZUAOooLQs9UdrAAfPr7JJtoM3Rv0=; b=gpUgRNGuQXt3ZKxIY9oNw/iXv06/d/Zd9CKMYw865VEJKxuRRLgNk9BKzHfdozKfH6 4S+t7O5Y3Ult/KTWFzJlUFQHHTn3a3SIs0gqH5F9ueYZ214qlYg7ikxVb96UiEOO/R++ 3bBKM9DsWFiw1qFIAM++EzzsY3uDJy7bpK+JylEHk5iIv+0wtWY+CFssYgdkyDMy0Tpd IWkRQHBkuDOuNNB8O/HIxRYkSwldg+861NTDEQHJfjUUtddw7zIlE1jc004JSDQawuhN eMCOjMGoTg0x6INGmzMEa3Mv58EaKVlPntqkhuaTJrCp2+LIkGHYddZrZeE9qdE+UcMj Qykg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769235657; x=1769840457; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=uaR31gnegNovKwQZUAOooLQs9UdrAAfPr7JJtoM3Rv0=; b=gOj35kx5GqQ6A3IM2CKldFoOd6pVpvfTQGseypZPsHfLmS2rmvCLK3tSQBzvS3YzP/ KuL+I81UoDbuPm6i5wiMqUERVavPL9cvR7aW1SW25frL/YyORm41PoFXss0f1Lnxp1gW 8TwyS/3T8GVUcOdMh8umtZhztigVSB9LrZqGe28UQMjw1K1NqQQbu3Z+LGfpnNebL10E QLUn1IUvL5uv0eYpftx6dvf/OdJSWBc0wvsfjmL8iL3XXIs708r3YZxdLJ3W5ZEqjpAt hVnwj6tBchBN2H/bq9zWNIMKKw7E6GYHWClJK+dumj2CrLE++m1RrDTVjjREZwXiDYBD ubMQ== X-Forwarded-Encrypted: i=1; AJvYcCW8bBptI/gx808PhUewpy+qZ94/UmIKSyGlTG4XpGmhbASkxrDaTdq/LyEtI7UjJnP6TItCW1XBkGDjjcY=@vger.kernel.org X-Gm-Message-State: AOJu0YxYTWJaU5+OA7ZkffmCERWL/oZ0RqOHkoNXIEaCZ/ue8AFAunS8 cGY3Zzq5hOvJENlkRR1GYu5x1k5SohYeycDLzLWfROAMlV03Qsabihlx X-Gm-Gg: AZuq6aIrEF4tl2d/7CuGymvZZ7xW2kHbuWYxFgEzchzf8VuAdkgYes8L7KFwGHSHQwy MiF8bvR34OdI59vQ+UjmzKRHNrXJPrmlspRuH8+VnEdvLVA95AoTU3eSrW2qYEYeQQR+1E3LiOI eWshgP6a2KXaJft9d2BjFpg37nfWpiVdC/xzwnAquITMPSSgtwu5CfjNA+yEECoaoyVlvc2HmGC oKn1D7O7bJp1DD0cHNfmJrlzGzeNaIqpkIWJ4MJAiB9PZR79S/Ufd5ShkjcpxADH33+gYcgY36u oKkOOjpWyGoiqJWuEWHpb4+RoNo7J6rE5pGxg/ZnBYvm3z+BzyRb5ofJ2nZZxtX4QFuz9SnWukd P5kobYoUOhyjOlhUVOriIiq2nceFFiO5uqjUpo+wvw1YTwVxA8YkdwolhonrjGk5CDU3hRr+gON r4WpbSCDtxjEKeYtdamIk= X-Received: by 2002:a17:90a:c110:b0:340:e4fb:130b with SMTP id 98e67ed59e1d1-3537955a310mr3049399a91.14.1769235657375; Fri, 23 Jan 2026 22:20:57 -0800 (PST) Received: from 7950hx ([103.173.155.241]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-353354a6b5asm6496537a91.9.2026.01.23.22.20.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Jan 2026 22:20:57 -0800 (PST) From: Menglong Dong X-Google-Original-From: Menglong Dong To: andrii@kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, davem@davemloft.net, dsahern@kernel.org, tglx@linutronix.de, mingo@redhat.com, jiang.biao@linux.dev, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, bpf@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH bpf-next v13 04/13] bpf: support fsession for bpf_session_is_return Date: Sat, 24 Jan 2026 14:19:59 +0800 Message-ID: <20260124062008.8657-5-dongml2@chinatelecom.cn> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260124062008.8657-1-dongml2@chinatelecom.cn> References: <20260124062008.8657-1-dongml2@chinatelecom.cn> 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 Content-Type: text/plain; charset="utf-8" If fsession exists, we will use the bit (1 << BPF_TRAMP_IS_RETURN_SHIFT) in ((u64 *)ctx)[-1] to store the "is_return" flag. The logic of bpf_session_is_return() for fsession is implemented in the verifier by inline following code: bool bpf_session_is_return(void *ctx) { return (((u64 *)ctx)[-1] >> BPF_TRAMP_IS_RETURN_SHIFT) & 1; } Signed-off-by: Menglong Dong Co-developed-by: Leon Hwang Signed-off-by: Leon Hwang --- v11: - rename BPF_TRAMP_SHIFT_IS_RETURN to BPF_TRAMP_IS_RETURN_SHIFT v10: - fix the wrong description of bpf_session_is_return() in commit log and comment - rename the prefix from BPF_TRAMP_M_ tp BPF_TRAMP_SHIFT_ - remove the definition of BPF_TRAMP_M_NR_ARGS - use 63 for the shift of BPF_TRAMP_SHIFT_IS_RETURN - check the program type in bpf_session_filter() v9: - remove the definition of bpf_fsession_is_return() v7: - reuse the kfunc bpf_session_is_return() instead of introduce new kfunc v4: - split out the bpf_fsession_cookie() to another patch v3: - merge the bpf_tracing_is_exit and bpf_fsession_cookie into a single patch v2: - store the session flags after return value, instead of before nr_args - inline the bpf_tracing_is_exit, as Jiri suggested --- include/linux/bpf.h | 2 ++ kernel/bpf/verifier.c | 13 +++++++++++++ kernel/trace/bpf_trace.c | 39 ++++++++++++++++++++++++++------------- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 41228b0add52..29eecd79352e 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1229,6 +1229,8 @@ enum { #endif }; =20 +#define BPF_TRAMP_IS_RETURN_SHIFT 63 + struct bpf_tramp_links { struct bpf_tramp_link *links[BPF_MAX_TRAMP_LINKS]; int nr_links; diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 0fa73d56cb8b..d04aea235a12 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -23011,6 +23011,19 @@ static int fixup_kfunc_call(struct bpf_verifier_en= v *env, struct bpf_insn *insn, desc->func_id =3D=3D special_kfunc_list[KF_bpf_rdonly_cast]) { insn_buf[0] =3D BPF_MOV64_REG(BPF_REG_0, BPF_REG_1); *cnt =3D 1; + } else if (desc->func_id =3D=3D special_kfunc_list[KF_bpf_session_is_retu= rn] && + env->prog->expected_attach_type =3D=3D BPF_TRACE_FSESSION) { + /* + * inline the bpf_session_is_return() for fsession: + * bool bpf_session_is_return(void *ctx) + * { + * return (((u64 *)ctx)[-1] >> BPF_TRAMP_IS_RETURN_SHIFT) & 1; + * } + */ + insn_buf[0] =3D BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); + insn_buf[1] =3D BPF_ALU64_IMM(BPF_RSH, BPF_REG_0, BPF_TRAMP_IS_RETURN_SH= IFT); + insn_buf[2] =3D BPF_ALU64_IMM(BPF_AND, BPF_REG_0, 1); + *cnt =3D 3; } =20 if (env->insn_aux_data[insn_idx].arg_prog) { diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 13f0a2de33b7..f7baeb8278ca 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -1286,7 +1286,8 @@ static bool is_kprobe_multi(const struct bpf_prog *pr= og) =20 static inline bool is_kprobe_session(const struct bpf_prog *prog) { - return prog->expected_attach_type =3D=3D BPF_TRACE_KPROBE_SESSION; + return prog->type =3D=3D BPF_PROG_TYPE_KPROBE && + prog->expected_attach_type =3D=3D BPF_TRACE_KPROBE_SESSION; } =20 static inline bool is_uprobe_multi(const struct bpf_prog *prog) @@ -1297,7 +1298,14 @@ static inline bool is_uprobe_multi(const struct bpf_= prog *prog) =20 static inline bool is_uprobe_session(const struct bpf_prog *prog) { - return prog->expected_attach_type =3D=3D BPF_TRACE_UPROBE_SESSION; + return prog->type =3D=3D BPF_PROG_TYPE_KPROBE && + prog->expected_attach_type =3D=3D BPF_TRACE_UPROBE_SESSION; +} + +static inline bool is_trace_fsession(const struct bpf_prog *prog) +{ + return prog->type =3D=3D BPF_PROG_TYPE_TRACING && + prog->expected_attach_type =3D=3D BPF_TRACE_FSESSION; } =20 static const struct bpf_func_proto * @@ -3341,34 +3349,39 @@ __bpf_kfunc __u64 *bpf_session_cookie(void *ctx) =20 __bpf_kfunc_end_defs(); =20 -BTF_KFUNCS_START(kprobe_multi_kfunc_set_ids) +BTF_KFUNCS_START(session_kfunc_set_ids) BTF_ID_FLAGS(func, bpf_session_is_return) BTF_ID_FLAGS(func, bpf_session_cookie) -BTF_KFUNCS_END(kprobe_multi_kfunc_set_ids) +BTF_KFUNCS_END(session_kfunc_set_ids) =20 -static int bpf_kprobe_multi_filter(const struct bpf_prog *prog, u32 kfunc_= id) +static int bpf_session_filter(const struct bpf_prog *prog, u32 kfunc_id) { - if (!btf_id_set8_contains(&kprobe_multi_kfunc_set_ids, kfunc_id)) + if (!btf_id_set8_contains(&session_kfunc_set_ids, kfunc_id)) return 0; =20 - if (!is_kprobe_session(prog) && !is_uprobe_session(prog)) + if (!is_kprobe_session(prog) && !is_uprobe_session(prog) && !is_trace_fse= ssion(prog)) return -EACCES; =20 return 0; } =20 -static const struct btf_kfunc_id_set bpf_kprobe_multi_kfunc_set =3D { +static const struct btf_kfunc_id_set bpf_session_kfunc_set =3D { .owner =3D THIS_MODULE, - .set =3D &kprobe_multi_kfunc_set_ids, - .filter =3D bpf_kprobe_multi_filter, + .set =3D &session_kfunc_set_ids, + .filter =3D bpf_session_filter, }; =20 -static int __init bpf_kprobe_multi_kfuncs_init(void) +static int __init bpf_trace_kfuncs_init(void) { - return register_btf_kfunc_id_set(BPF_PROG_TYPE_KPROBE, &bpf_kprobe_multi_= kfunc_set); + int err =3D 0; + + err =3D err ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_KPROBE, &bpf_sessi= on_kfunc_set); + err =3D err ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_sess= ion_kfunc_set); + + return err; } =20 -late_initcall(bpf_kprobe_multi_kfuncs_init); +late_initcall(bpf_trace_kfuncs_init); =20 typedef int (*copy_fn_t)(void *dst, const void *src, u32 size, struct task= _struct *tsk); =20 --=20 2.52.0