From nobody Mon Feb 9 10:28:23 2026 Received: from mail-yw1-f195.google.com (mail-yw1-f195.google.com [209.85.128.195]) (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 6005CF9C0 for ; Thu, 8 Jan 2026 02:25:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.195 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767839152; cv=none; b=k7cSom9hmUaV+QL6/6xpw5LPlJwkNL5QrTpHj1Veodzjpmtw6qCY0MA9rzt6iTWaoURFa1UKlbJShgRjlHLt6Mz9UbbvnahNtuL7hrtAB32phRtQDRckxntooDq5CsQwanWjXYey1ICTjSIe047hBizWXNw19Swq+I+Lw1p4EoI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767839152; c=relaxed/simple; bh=33ZXMJWuCdojUF0ws2zQppZFn+bzB7mAX+06WHAujaY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Am9xQDkbZOsCNkstLBi1O8OzM3u3UZDOZTjji8Vq6yBj2YLS81ZdvjRNeda7e2RLE6rk5LfLfcfhRxvzvkIT6bPtYDSmKs190uNuvMwdAkQCK71kr5AeUBGbyNH+2/kwO4iIuMXkg592fRXJfCMKm3HZUlCSjrrlh6Re02mvnnc= 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=W6klOGME; arc=none smtp.client-ip=209.85.128.195 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="W6klOGME" Received: by mail-yw1-f195.google.com with SMTP id 00721157ae682-78fc7893c93so28600597b3.2 for ; Wed, 07 Jan 2026 18:25:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1767839148; x=1768443948; 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=vInP6QyodjCROjJEkJgbYkQF4JZFktsL3Kjqjh+D3Lw=; b=W6klOGMEG+C/PvDfFt72phtgKdDwPgV65g4iKCaduLYOAoJBnDlblGbyR9YXSonJuT EIAowoEtSNeOSqfRSXuY5LBCKo9LWkBuapI/51NmmIDFKDjGTYftcuWo5r57adFO4XUm bOpSd2ZGh4PLQkf62Ex+x0ofGX36uqbIVNKy6FWB4Eu8tAyYzSS7x7HGZDce8sV6HV9B Lke5LzT+wqG0ap7r6M84673d588WaV8l6F7Pf0V2LYcAFoLzSxqE1jxwT7GwRobF5Dui 24P5QnknGvlPodZq5ALNHGEiJOFREgit38bjzUSOCNaOZTKHLnHBq/k3N29widOQtlFl ttGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767839148; x=1768443948; 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=vInP6QyodjCROjJEkJgbYkQF4JZFktsL3Kjqjh+D3Lw=; b=w52dw2kKvA4hALgdxS8O2Jwh1OXH8T6nKH5cVDxA5MvxqBDJrFGw/f7khJm475mozx 9YCxs3iWHLHLjd1UBwGvSRa8FCV8qXmmadQ3ilfhgNb7Gr6SAzRGQd5Qv0AdNC23BSbI s6xbCibQuo1BhoCj+8jrkHLTRCBeV42n2jo0Leurln838UrVQSp4Zys8GY+hf62cTcv/ RSsvh76S3fma6jvGW7XTXCh519S25kXd6ytaJ2zaoBpkExf8koBRxXdt2KFUOoeZ3Bt4 hOnHS+jVvqhTbNgx7sQm0FROcJKQ/TiCEpVYOD2qu9u+rpWYIlUAOZUlVQ+D/H5v3AeN gAPA== X-Forwarded-Encrypted: i=1; AJvYcCUl3uLzFs8v13/j74WaDB17Phb20mBf3UgsLoyYs2nOwI32beKbOHUgFvThQBNWNApnuTeb0FJIaBTRGrM=@vger.kernel.org X-Gm-Message-State: AOJu0YzA12j5ASeh5rsYPOtw5zZlTEUJ41pZEfFR4TUL84ziucdYqv+6 FEMoK9wnihVhnv5jHLAI/gVLrgB5khbUnnGJPYyutQ9N5+Y/aZXHpCay X-Gm-Gg: AY/fxX4t41pTbpdRt4uQH8iyXU0QTJH/57wH+KD078E9Esqdc6MlbK/XNVhUfFK5LfY n54zpdBLfsLsStggziMfkmoU12lHc/cX0QCFzuk/BFXAzv4/ItF94xBNafsYGO1N8kT+fqKvliO baWsQWhPy2AEJ7q1OtTHra5T9X6JFsDFFG8+JGMcI8uikvdSVRwJbOCYvAiGt5Y9UIIIfGjChkm DTim3gxNiXy2pH5FgbyhJDQXuT1APVNZTmfe81C+gAo8MwnavCoVu+3I37BimFS9YwkjJ5QQXry usXa5EnEvu0WbsR/3pekcXAM60sXgdTAHVpHFRzlhEi1Ambite+cbw4RbMVzjZf/xuEn+I8ks+O nYPcJ7ug8JqMEh89YIDrvluXnHLj29nBIryRiqnwjCWB2RhSOh9i0YgaOJ+QvVZjuz7fLMXoAEA k1lF4nsn0= X-Google-Smtp-Source: AGHT+IHFgjTB5e0Gs+yTOGkTyHd71ZhpHmUP1hLLO3sarTu+JAfQiodWZuFVGmYj6NKPXu4GfA9HHA== X-Received: by 2002:a05:690c:600a:b0:78c:2916:3ef5 with SMTP id 00721157ae682-790b55c08a5mr42635457b3.8.1767839148092; Wed, 07 Jan 2026 18:25:48 -0800 (PST) Received: from 7940hx ([23.94.188.235]) by smtp.gmail.com with ESMTPSA id 00721157ae682-790aa57deacsm24855027b3.20.2026.01.07.18.25.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jan 2026 18:25:47 -0800 (PST) From: Menglong Dong X-Google-Original-From: Menglong Dong To: ast@kernel.org, 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 v8 04/11] bpf: support fsession for bpf_session_is_return Date: Thu, 8 Jan 2026 10:24:43 +0800 Message-ID: <20260108022450.88086-5-dongml2@chinatelecom.cn> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260108022450.88086-1-dongml2@chinatelecom.cn> References: <20260108022450.88086-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_M_IS_RETURN) in ctx[-1] to store the "is_return" flag. Introduce the function bpf_fsession_is_return(), which is used to tell if it is fexit currently. Meanwhile, inline it in the verifier. The calling to bpf_session_is_return() will be changed to bpf_fsession_is_return() in verifier for fsession. Signed-off-by: Menglong Dong Co-developed-by: Leon Hwang Signed-off-by: Leon Hwang --- 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 | 5 +++++ kernel/bpf/verifier.c | 15 ++++++++++++++- kernel/trace/bpf_trace.c | 34 +++++++++++++++++++++++----------- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 41228b0add52..95248b0d28ab 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1229,6 +1229,9 @@ enum { #endif }; =20 +#define BPF_TRAMP_M_NR_ARGS 0 +#define BPF_TRAMP_M_IS_RETURN 8 + struct bpf_tramp_links { struct bpf_tramp_link *links[BPF_MAX_TRAMP_LINKS]; int nr_links; @@ -3974,4 +3977,6 @@ static inline int bpf_map_check_op_flags(struct bpf_m= ap *map, u64 flags, u64 all return 0; } =20 +bool bpf_fsession_is_return(void *ctx); + #endif /* _LINUX_BPF_H */ diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index bfff3f84fd91..d3709edd0e51 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -12374,6 +12374,7 @@ enum special_kfunc_type { KF_bpf_arena_alloc_pages, KF_bpf_arena_free_pages, KF_bpf_arena_reserve_pages, + KF_bpf_session_is_return, }; =20 BTF_ID_LIST(special_kfunc_list) @@ -12451,6 +12452,7 @@ BTF_ID(func, bpf_task_work_schedule_resume_impl) BTF_ID(func, bpf_arena_alloc_pages) BTF_ID(func, bpf_arena_free_pages) BTF_ID(func, bpf_arena_reserve_pages) +BTF_ID(func, bpf_session_is_return) =20 static bool is_task_work_add_kfunc(u32 func_id) { @@ -12505,7 +12507,8 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env, struct bpf_reg_state *reg =3D ®s[regno]; bool arg_mem_size =3D false; =20 - if (meta->func_id =3D=3D special_kfunc_list[KF_bpf_cast_to_kern_ctx]) + if (meta->func_id =3D=3D special_kfunc_list[KF_bpf_cast_to_kern_ctx] || + meta->func_id =3D=3D special_kfunc_list[KF_bpf_session_is_return]) return KF_ARG_PTR_TO_CTX; =20 if (argno + 1 < nargs && @@ -22440,6 +22443,9 @@ static int specialize_kfunc(struct bpf_verifier_env= *env, struct bpf_kfunc_desc } else if (func_id =3D=3D special_kfunc_list[KF_bpf_arena_free_pages]) { if (env->insn_aux_data[insn_idx].non_sleepable) addr =3D (unsigned long)bpf_arena_free_pages_non_sleepable; + } else if (func_id =3D=3D special_kfunc_list[KF_bpf_session_is_return]) { + if (prog->expected_attach_type =3D=3D BPF_TRACE_FSESSION) + addr =3D (unsigned long)bpf_fsession_is_return; } desc->addr =3D addr; return 0; @@ -22558,6 +22564,13 @@ 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) { + /* Load nr_args from ctx - 8 */ + 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_M_IS_RETURN); + 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 297dcafb2c55..056f30844de2 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -3314,6 +3314,12 @@ static u64 bpf_uprobe_multi_entry_ip(struct bpf_run_= ctx *ctx) } #endif /* CONFIG_UPROBES */ =20 +bool bpf_fsession_is_return(void *ctx) +{ + /* This helper call is inlined by verifier. */ + return !!(((u64 *)ctx)[-1] & (1 << BPF_TRAMP_M_IS_RETURN)); +} + __bpf_kfunc_start_defs(); =20 __bpf_kfunc bool bpf_session_is_return(void *ctx) @@ -3334,34 +3340,40 @@ __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) && + prog->expected_attach_type !=3D BPF_TRACE_FSESSION) 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