From nobody Mon Feb 9 16:51:53 2026 Received: from mail-pf1-f193.google.com (mail-pf1-f193.google.com [209.85.210.193]) (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 73A673321DE for ; Fri, 23 Jan 2026 07:36:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.193 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769153781; cv=none; b=EQqv+JGgVkOuCNA1LFkGArxVMOTHYjpQDOCoVJYLJghQM225/cREC+Oe/8zvQNdfd2yWMJr+Wft1a5SwlCSOC5ysjvURc5VIlWxzKsW/Eueb+1mbc+Vf+YTwmDlWlTVO2wsrNZmd3hZ/0j0aNoBZo2Joo8/qRQ/u39ju8JYY8/k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769153781; c=relaxed/simple; bh=+LohW/5iZ/BTA3wEhODcalmnhVN7Hw9KbOPLybwo5cM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KujiBwRooh6Xcc7mui3L+HzW9UKgwr+QUe+GXcNDlT/N1yztPcMPZVINYs8b42dFavi/LYhRqKgx2cFSyNL6uPV0UAu8kLyd8GglRHDwqUUXKOBsCL7e56O+JOMaV/GJQkkf06H1rU5tvEcnxVm8t/ybOFashi4Yult2fUKTo9o= 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=Cyc0wtaZ; arc=none smtp.client-ip=209.85.210.193 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="Cyc0wtaZ" Received: by mail-pf1-f193.google.com with SMTP id d2e1a72fcca58-81f39438187so1047130b3a.2 for ; Thu, 22 Jan 2026 23:36:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769153777; x=1769758577; 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=zK61mrn2X2UJzXm7UaRNg+WFPm1j9ZsyaLN3K5LWzAA=; b=Cyc0wtaZTD6fQ+1bxDnH6I3L1JM32xAsnbfDk1pO3HPQ29OSS0Hsv1sBzCadgpN+tJ 1U36eNY6l6qviNYwUYZ/ldOWSwfs0FVc6vHVXUeA6pLnT19O87xdc6+/+im9HmsuEiEj tqn63851rBCO/U24NHXgGA77DoEQKhYbU3YwGLxPlVhdy4C3bxmdo5NcRqXrf+3hYX/b oDZWTD/XMN0xZt+N0wY20eNtwMsjIPob2kQhtGyOOSq8dkK0XUZJzQD3gHSl6742rhq8 uGA1NwkG7GFf9d+xKISs741cQoBDDZ6GntKkwEElhinNhl71QJSsL3+aszXzdKcmq32U ZD5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769153777; x=1769758577; 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=zK61mrn2X2UJzXm7UaRNg+WFPm1j9ZsyaLN3K5LWzAA=; b=jIyWvmb+l8jyBFQ8Xi7j3oP+rJLXvikjF+hDIPomBK5aENFpqQgQFq/jbLarYBmLSc 4++GVgFLd+vOaRVDcn/XQ4wSU9cXFyalSbixEzUy56bdrDAC+PmG75DC5h5q7MLRhnjA OIcEgSObqskf5WIK+IMffzsahC59ebv0bW+NTdRO+aFeVok1viS2HBvr62UvNj78zqmH ANcXTokFcYu1QvNx9AVi2lZeOc5aKCzsE+gzWM8NYjHMSRYqQVTDN26PcV1P8O8vuFOi FVBPZxweVZjTjC7Ouy6lTYnIx3W/sfieQCF5rfcK781gVWvPYfQC9iUzze0WtF0QyHTu CLPQ== X-Forwarded-Encrypted: i=1; AJvYcCXzmgV2uPA6PTrFllxocf8kxuWRV6uASQzAtayb11476xtk5Ki+pJEvqY5l8IVh/Y3GvzAPTBJnjuDKU2E=@vger.kernel.org X-Gm-Message-State: AOJu0YzLGv1PQMtPjJjgYuPjpJJ5W/0aeY/nLFVWskDU+wsz1jl5uH2K FArU2PErRE2KU0DrL513gDi64uL9CyfBjAML3nANiBop9sJVTuVgiokz X-Gm-Gg: AZuq6aISBpe5uxCZVzQ1Mgekji7kODgdSsi77StwtMt51sqcG18tmEZKm37nqhXCSPA QFAO2t5dK12xC9Ya/wqOg5WXkGMU8oN/ulUTn91pYi0+ob5s1hEsP3AXp6s+BdfOY0xJ3RBRy1X 7Kg2ivTLfIUnEr66ZG5hTdglBV81CucShRr7K0Xb/TTQ+t1XiJfSvhrbecxKnj7MErdvX92ZCNY vq0F5n5UhScoArkAtUKCaEw5FkJRZf0tEYbVKLyldGm7u4fjLmLTmN/1PYwa2x+KuFD2LmuUrrx FK67ux0o5ZY1MHEmuBxREwYPgOakd4Xj8rKNbzVtizo/IjYh9BpGdkHMc2+bc1dkzs7r8GDHH0S HydqSaImsAYtjKbuNxmfjX4ByYjuCluTA6/E9so8X0K7IC73huwBbw5NdGoPqqD/lTzgmVA0AOC DEiCU6CQsE X-Received: by 2002:a05:6a00:ad1:b0:81f:3a05:8a7a with SMTP id d2e1a72fcca58-82317d23940mr1878480b3a.19.1769153777539; Thu, 22 Jan 2026 23:36:17 -0800 (PST) Received: from 7940hx ([103.173.155.241]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-8231876fd1fsm1334205b3a.69.2026.01.22.23.36.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Jan 2026 23:36:17 -0800 (PST) From: Menglong Dong X-Google-Original-From: Menglong Dong To: andrii@kernel.org Cc: 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 v11 04/12] bpf: support fsession for bpf_session_is_return Date: Fri, 23 Jan 2026 15:35:24 +0800 Message-ID: <20260123073532.238985-5-dongml2@chinatelecom.cn> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260123073532.238985-1-dongml2@chinatelecom.cn> References: <20260123073532.238985-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 19fd16697262..2ae8f29b854f 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