From nobody Mon Feb 9 16:17:26 2026 Received: from mail-pf1-f194.google.com (mail-pf1-f194.google.com [209.85.210.194]) (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 DC1513451AE for ; Sat, 10 Jan 2026 14:12:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.194 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768054335; cv=none; b=RBFGN76k0RY6dvVbVQV0qd6cm6N23Bi29XEV2FwJyK8AnsSR6ICazyNDM1aCkGgTmowpMUyPBaQ1ouXtBApl3b2T9Bn12DRRZyYF1Vkc1o+6fBjXl413gWAUx6IhQxsBsfUvmpWYDC1d4rDifOtKz4IHzHamwL/ewW/re5IUlFc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768054335; c=relaxed/simple; bh=n/7k5mO/6SZ7HqVRB1N/I0OmYGbrp12aFWUTdPqo8s0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dQVphcYGkDklU/QHRM51zDb9yOB0r9RSNoI1O3nJ8sChy2kF6RUSePxjKhCXcFsHKWvkMLaTUlXWIluQzliZ6nnPf/6gm4shugQI2JIQuB9Llw8iNBajBYnmcmZY7w3PqqYUmxYYVHYtB+kXstM+fq1SJQPCDpITotG/VAFrYxk= 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=EEPmuRAV; arc=none smtp.client-ip=209.85.210.194 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="EEPmuRAV" Received: by mail-pf1-f194.google.com with SMTP id d2e1a72fcca58-7b8eff36e3bso5046769b3a.2 for ; Sat, 10 Jan 2026 06:12:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1768054333; x=1768659133; 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=+zpUBClHIGdPZIQ45WTEZS7Nhxwojh2mUAy6TAH9Jbw=; b=EEPmuRAVPBkkl34bfvurCb1Dfru1a8+WQsfpDTuoTxLwK9YhBkIKxGCq5KJ9iDPGyE f/diZaTZYPoQcF7l72iB0KWKfvk4w9XqWx0GOW+Bp+3E3vdDsWt1Pz8YYZuSgFCrorbT CeOJWWkCWfplsqzR7FpjbkJ7xozn/HgY6JzneeQaENbdp28TSetDw36u56jF7angTOop hiL0NcUPj9OmvoZyG6Ob7SEv+WPjXMKDTB3r+pVckzTkl1+5QqSlkjsXQCZ1HgTXwB+A Fx5UMmjbKUutaellgpOQKGDDTrvvdUQ4uaDhyZYEy7Cdzz0Ewrzkc7zBZcNBQhPZGoXh o6tA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768054333; x=1768659133; 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=+zpUBClHIGdPZIQ45WTEZS7Nhxwojh2mUAy6TAH9Jbw=; b=kSk/gTG6ioA5LtYv34OuQNvVqLc5c1vTmxaCDZSkQ07tJdLHfGfvb4XlcXOWn0BEuE fNMriLEVnjhv9Zb0Et8VoA7VkIy7pUVM/vWq5shxGAm4+r/Rv7ILbSB6eOk4rEXzV77K B6rvydTl4wsos7baExL3hrvqGWo3eOJzBcM04u6arkBz0Ep5dn63O2JfLATQh3jck9CR nk51gd48GzunPncInJpMlIrZBJcuZ5iyHLexRybCxULGvLXVAeu75gVbmzwQMBE7MFe4 iXhWSZpruswJKo/S/bcHKbYt2WHWgmOAvZfkXR8tNkQYaoOTGmkb/UTwdr+Gh0kuDcrn m/VQ== X-Forwarded-Encrypted: i=1; AJvYcCVZ3xqpTzQAWNTLvcJ/7tQq/o42XaV9fWJ1XNLqL8yfQmjcF+HsOdcbEj71QB8QybstnKOgAbrDXk0WEZA=@vger.kernel.org X-Gm-Message-State: AOJu0Ywa9xX/n4jLrlw6Y7TtjbaDbJsEcyZvJEeZSm8eFZknyN5mzefn 3s4GAqaC7uoSlVVelhhxViSzpk3R4uzNbzeF5prOP/XMi9lkCwgJswan10KgyANmn99+WA== X-Gm-Gg: AY/fxX7DBF1I6Psp1kOw5WmvnZh+qgETR/hk6VoAESiRGQJpkK8HkCsjTXvPo8zI0ov ANfaj96XDQP85eMkQjO9/PBPMUWiMRVFRGQv2MgVLerHwF5XyxBeqGp2AcF3NyqjdVAKx9Owtu2 /6d57DgF1lRT6jY0FNItS42WmvhL7RWydxErWW0zuP68ucmaBosNpfpqLCUM3QYz2FrnEURIIPI RwTpiQgef88LHs3M4rYjrl4RKBgTmgyXFUjt8Asa39r3veOANzsUR2tddJ+/YVFdEuNeLQseWtv OBZS6DFh7JaBhqR3Cik3aPsqXOgwps23lgV+8Ae3i6sWH6x6nOcEG54RhUvmKbTpIyJOhXSiB7k xxDKFqt0NZGcZqv1+munNS7ylNvEvciVdC4yXckfojCM7+EMU7iM7D1xeevH5doBnDagbvsVSm1 +7/bLBt+Q= X-Google-Smtp-Source: AGHT+IG31oLOfstQo576BCLakdE0G7b1VJi9fSiQv5hvsVWJVmiQKV1SlMPR10Y/aPftZLJljsKelA== X-Received: by 2002:a05:6a00:f94:b0:81e:f623:ba0c with SMTP id d2e1a72fcca58-81ef623bc8dmr3387614b3a.44.1768054333195; Sat, 10 Jan 2026 06:12:13 -0800 (PST) Received: from 7950hx ([160.187.0.149]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-81f42658f03sm1481079b3a.20.2026.01.10.06.12.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 10 Jan 2026 06:12:12 -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 v9 04/11] bpf: support fsession for bpf_session_is_return Date: Sat, 10 Jan 2026 22:11:08 +0800 Message-ID: <20260110141115.537055-5-dongml2@chinatelecom.cn> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260110141115.537055-1-dongml2@chinatelecom.cn> References: <20260110141115.537055-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. 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] & (1 << BPF_TRAMP_M_IS_RETURN)); } Signed-off-by: Menglong Dong Co-developed-by: Leon Hwang Signed-off-by: Leon Hwang --- 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 | 3 +++ kernel/bpf/verifier.c | 15 ++++++++++++++- kernel/trace/bpf_trace.c | 28 +++++++++++++++++----------- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 41228b0add52..2640ec2157e1 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; diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index bfff3f84fd91..1b0292a03186 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 && @@ -22558,6 +22561,16 @@ 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) { + /* implement and inline the bpf_session_is_return() for + * fsession, and the logic is: + * return !!(((u64 *)ctx)[-1] & (1 << BPF_TRAMP_M_IS_RETURN)) + */ + 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..1fe508d451b7 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -3334,34 +3334,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