From nobody Mon Feb 9 15:08:37 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 CCB4713E02A for ; Sun, 8 Feb 2026 05:33:51 +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=1770528831; cv=none; b=koYTMI+wGBNfL1S4bPsrkhRnw/pNhIOc4aog7cJpGXp6rrkjiCHOuXpI7p0rI5/BXNXowE+s4vEHkFNM7d/VeCdvxyWBfVVzHrovGrSulWOhnM4CIraU29w6Lq1yHPYpSLKLnXFZcsnE0cPo12dCrfxnOTLcOUMTqRZLmNhNc9o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770528831; c=relaxed/simple; bh=NdJfE6UGkldmnYF5SJP0QfORXRuqGjkBEOg/jafjqns=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=K6g38i0FqUrvROviewTf0Sx5p1HtpwB1i9nqLv5ulKR5QAspJakP2vLF1Dw7xV0GO/XGpIa/7KbNoOCSziB0iLza2f/oVdIIODrOk68EEDq5Y6BPuW5gPk/vl4JExoTaK9xklc+eP4WTKW9jl3Xe1sdQ4gPIz5Jby2Qvx7+idsw= 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=JwreA6id; 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="JwreA6id" Received: by mail-pf1-f194.google.com with SMTP id d2e1a72fcca58-81f4ba336b4so2746869b3a.1 for ; Sat, 07 Feb 2026 21:33:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1770528831; x=1771133631; 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=uauA93iGE6XFs9gPgdNEdhBRsdXEO6OJ0ct51+bwGaI=; b=JwreA6idY1BlNaV5d+8ayDx3Z2DRsxd4NpIUri49PJTyMbzxJh+aDksR/gMhX6JAjE Pv18FNuStEo10nJ/01A34V9CsM7XPd3Jo+5LYgDf/ZJiWEdXApyRWZO7z9nqNCFspLOL I7rZr9NK5yodI5/tZ7IY8wwC480AtIHMxxYrR7PaxFsHrRSObP0/orloqz4iwQUzKx0M Fz4h+SQS3e0HEsWdwphh2BdINkenVgpYLiGTY/mUwTgPJ+lckt1w5Dw4Qdp94ygmA5T6 7ppKGV+mjZqKMxtfYR3MBXOmGm5MZSq0qDRWR+Uc19XiHIaaB9LFvXXld1Z5EHoyZNrI rFww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770528831; x=1771133631; 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=uauA93iGE6XFs9gPgdNEdhBRsdXEO6OJ0ct51+bwGaI=; b=WqqmTyALGlj1V3ayJfAYBV/OqDsDRv56LusEKU+dCfZ6D3mbhvj4GDa3KLO2JSrpGk WE3N0KJbvFzw/ZG93wh48apKABO/S8zffiCWhSYryEpgZ+uoBhf+dIGS8w3OrgVSDuqN KoW4lCqJfEUObfPUhkthUc/W3nATO/2MBdWP7ArRhBuUJIZ+97k4SksAA3QkrjnIMKbH ICAwC4FaMtxn8/hQ8wlx3qMHS8Y8agpuXJtEbRD+STW0FgMBhCcrZ62rATXSBa8c1MIz 6zIweM7bZ6duu0tlUAYP3EqguOVSi0LzAU1xDG7nCRvJTc8XvnKpgssC8Y8rvaZkiGGY cqQw== X-Forwarded-Encrypted: i=1; AJvYcCVn0q1YFD52cNC+SwjaCFr6ZEGfrmehOzCualclLbDr2snLpJmP+Va3K05s9G8MSm1PvS0eb/sugF28O+s=@vger.kernel.org X-Gm-Message-State: AOJu0YzC1p3nb2z0NaVbxCAWmdW8apUA4T9+iRNIW4WjUYQ5tMaS4sNR wp1ThZnfDIeQ6XUQI7GcSCRdJYw+/2mf0+yMpPiyAYIKd+phZ7i1txbt X-Gm-Gg: AZuq6aJcI7q58/bXKXB0byuPSg2JFS1ixpZXaqdTMOjE7N7DAL1aDieriznvAN/oal6 o0FpHN/SnRa615cW5gQ2EOBsSxewFZ3SbKX68soqCUmqxCT6RAJtpqAfXS51AxJgaaCDOzvUMBZ N/THaybckdPxOC6KKpv7KbCII2GNqlLcTJMjPEb2S/zwn/OxFA2v7iwBZIP+HKZFfnzsfes0Nkr t9GmInfpmQCoOVPSSTYzM9FhLGaz9yualR/KppX0XpaPqqJNSH4yFQHn77E0lTTFPzjj3Xuxo3/ UgxQdDQU4blcZVcFQoBoYcwet6no7RLaoTZ5sCzC0xWA9nqW8lNq1YcDZAnEIrek/r2ThzdTjv1 BXs+6SghxVpscd55WS/x+KEZQ5ht4UMCYKalH9jLCbdVjRKg/xtWh0IXwINNJXO16CsXXC4XMYx v/z9TIVnnhNKuP0EqZ0KU= X-Received: by 2002:a05:6a00:3981:b0:81f:517a:56e7 with SMTP id d2e1a72fcca58-82441655315mr6790986b3a.30.1770528831225; Sat, 07 Feb 2026 21:33:51 -0800 (PST) Received: from 7950hx ([103.173.155.241]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-824418de80dsm6067700b3a.62.2026.02.07.21.33.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Feb 2026 21:33:50 -0800 (PST) From: Menglong Dong X-Google-Original-From: Menglong Dong To: ast@kernel.org, bjorn@kernel.org Cc: daniel@iogearbox.net, andrii@kernel.org, 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, pulehui@huawei.com, puranjay@kernel.org, pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, bpf@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, jiang.biao@linux.dev Subject: [PATCH bpf-next v4 2/3] bpf, riscv: add fsession support for trampolines Date: Sun, 8 Feb 2026 13:33:10 +0800 Message-ID: <20260208053311.698352-3-dongml2@chinatelecom.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260208053311.698352-1-dongml2@chinatelecom.cn> References: <20260208053311.698352-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-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Implement BPF_TRACE_FSESSION support in the RISC-V trampoline JIT. The logic here is similar to what we did in x86_64. In order to simply the logic, we factor out the function invoke_bpf() for fentry and fexit. Signed-off-by: Menglong Dong Tested-by: Bj=C3=B6rn T=C3=B6pel Acked-by: Bj=C3=B6rn T=C3=B6pel --- v4: - fix the build error v3: - remove the "always" from the comment v2: - use bpf_prog_calls_session_cookie() in invoke_bpf() --- arch/riscv/net/bpf_jit_comp64.c | 74 ++++++++++++++++++++++++++++----- 1 file changed, 64 insertions(+), 10 deletions(-) diff --git a/arch/riscv/net/bpf_jit_comp64.c b/arch/riscv/net/bpf_jit_comp6= 4.c index e4f45e2e7e2f..f065b45e8d8f 100644 --- a/arch/riscv/net/bpf_jit_comp64.c +++ b/arch/riscv/net/bpf_jit_comp64.c @@ -996,6 +996,29 @@ static int invoke_bpf_prog(struct bpf_tramp_link *l, i= nt args_off, int retval_of return ret; } =20 +static int invoke_bpf(struct bpf_tramp_links *tl, int args_off, int retval= _off, + int run_ctx_off, int func_meta_off, bool save_ret, u64 func_meta, + int cookie_off, struct rv_jit_context *ctx) +{ + int i, cur_cookie =3D (cookie_off - args_off) / 8; + + for (i =3D 0; i < tl->nr_links; i++) { + int err; + + if (bpf_prog_calls_session_cookie(tl->links[i])) { + u64 meta =3D func_meta | ((u64)cur_cookie << BPF_TRAMP_COOKIE_INDEX_SHI= FT); + + emit_store_stack_imm64(RV_REG_T1, -func_meta_off, meta, ctx); + cur_cookie--; + } + err =3D invoke_bpf_prog(tl->links[i], args_off, retval_off, run_ctx_off, + save_ret, ctx); + if (err) + return err; + } + return 0; +} + static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, const struct btf_func_model *m, struct bpf_tramp_links *tlinks, @@ -1005,13 +1028,15 @@ static int __arch_prepare_bpf_trampoline(struct bpf= _tramp_image *im, int i, ret, offset; int *branches_off =3D NULL; int stack_size =3D 0, nr_arg_slots =3D 0; - int retval_off, args_off, nregs_off, ip_off, run_ctx_off, sreg_off, stk_a= rg_off; + int retval_off, args_off, func_meta_off, ip_off, run_ctx_off, sreg_off, s= tk_arg_off; + int cookie_off, cookie_cnt; struct bpf_tramp_links *fentry =3D &tlinks[BPF_TRAMP_FENTRY]; struct bpf_tramp_links *fexit =3D &tlinks[BPF_TRAMP_FEXIT]; struct bpf_tramp_links *fmod_ret =3D &tlinks[BPF_TRAMP_MODIFY_RETURN]; bool is_struct_ops =3D flags & BPF_TRAMP_F_INDIRECT; void *orig_call =3D func_addr; bool save_ret; + u64 func_meta; u32 insn; =20 /* Two types of generated trampoline stack layout: @@ -1042,10 +1067,14 @@ static int __arch_prepare_bpf_trampoline(struct bpf= _tramp_image *im, * [ ... ] * FP - args_off [ arg1 ] * - * FP - nregs_off [ regs count ] + * FP - func_meta_off [ regs count, etc ] * * FP - ip_off [ traced func ] BPF_TRAMP_F_IP_ARG * + * [ stack cookie N ] + * [ ... ] + * FP - cookie_off [ stack cookie 1 ] + * * FP - run_ctx_off [ bpf_tramp_run_ctx ] * * FP - sreg_off [ callee saved reg ] @@ -1077,14 +1106,20 @@ static int __arch_prepare_bpf_trampoline(struct bpf= _tramp_image *im, stack_size +=3D nr_arg_slots * 8; args_off =3D stack_size; =20 + /* function metadata, such as regs count */ stack_size +=3D 8; - nregs_off =3D stack_size; + func_meta_off =3D stack_size; =20 if (flags & BPF_TRAMP_F_IP_ARG) { stack_size +=3D 8; ip_off =3D stack_size; } =20 + cookie_cnt =3D bpf_fsession_cookie_cnt(tlinks); + /* room for session cookies */ + stack_size +=3D cookie_cnt * 8; + cookie_off =3D stack_size; + stack_size +=3D round_up(sizeof(struct bpf_tramp_run_ctx), 8); run_ctx_off =3D stack_size; =20 @@ -1132,10 +1167,19 @@ static int __arch_prepare_bpf_trampoline(struct bpf= _tramp_image *im, if (flags & BPF_TRAMP_F_IP_ARG) emit_store_stack_imm64(RV_REG_T1, -ip_off, (u64)func_addr, ctx); =20 - emit_store_stack_imm64(RV_REG_T1, -nregs_off, nr_arg_slots, ctx); + func_meta =3D nr_arg_slots; + emit_store_stack_imm64(RV_REG_T1, -func_meta_off, func_meta, ctx); =20 store_args(nr_arg_slots, args_off, ctx); =20 + if (bpf_fsession_cnt(tlinks)) { + /* clear all session cookies' value */ + for (i =3D 0; i < cookie_cnt; i++) + emit_sd(RV_REG_FP, -cookie_off + 8 * i, RV_REG_ZERO, ctx); + /* clear return value to make sure fentry always get 0 */ + emit_sd(RV_REG_FP, -retval_off, RV_REG_ZERO, ctx); + } + if (flags & BPF_TRAMP_F_CALL_ORIG) { emit_imm(RV_REG_A0, ctx->insns ? (const s64)im : RV_MAX_COUNT_IMM, ctx); ret =3D emit_call((const u64)__bpf_tramp_enter, true, ctx); @@ -1143,9 +1187,9 @@ static int __arch_prepare_bpf_trampoline(struct bpf_t= ramp_image *im, return ret; } =20 - for (i =3D 0; i < fentry->nr_links; i++) { - ret =3D invoke_bpf_prog(fentry->links[i], args_off, retval_off, run_ctx_= off, - flags & BPF_TRAMP_F_RET_FENTRY_RET, ctx); + if (fentry->nr_links) { + ret =3D invoke_bpf(fentry, args_off, retval_off, run_ctx_off, func_meta_= off, + flags & BPF_TRAMP_F_RET_FENTRY_RET, func_meta, cookie_off, ctx); if (ret) return ret; } @@ -1192,9 +1236,14 @@ static int __arch_prepare_bpf_trampoline(struct bpf_= tramp_image *im, *(u32 *)(ctx->insns + branches_off[i]) =3D insn; } =20 - for (i =3D 0; i < fexit->nr_links; i++) { - ret =3D invoke_bpf_prog(fexit->links[i], args_off, retval_off, - run_ctx_off, false, ctx); + /* set "is_return" flag for fsession */ + func_meta |=3D (1ULL << BPF_TRAMP_IS_RETURN_SHIFT); + if (bpf_fsession_cnt(tlinks)) + emit_store_stack_imm64(RV_REG_T1, -func_meta_off, func_meta, ctx); + + if (fexit->nr_links) { + ret =3D invoke_bpf(fexit, args_off, retval_off, run_ctx_off, func_meta_o= ff, + false, func_meta, cookie_off, ctx); if (ret) goto out; } @@ -2094,3 +2143,8 @@ bool bpf_jit_inlines_helper_call(s32 imm) return false; } } + +bool bpf_jit_supports_fsession(void) +{ + return true; +} --=20 2.53.0