From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 94858E83056 for ; Tue, 3 Feb 2026 06:21:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:From:References:Cc:To: Subject:MIME-Version:Date:Message-ID:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=u0LbyDuYwHfvzcJDCKU30dooEQneB5uSFT4/BTNg2aM=; b=afZOjuNR1CdjYN jbSuHnHuXnzXmUqanuozT38lCfUX1y0Smj1U8WsghEMRqkqZbUoCSFx2jU4NjSEXVjZgnu7N5f2sY Qfpit3Dwr4QaSbewxfJLIK/4DK87nyoHIYtxAo38h6rMSV3/8L6ihQ1C86R4gEkjQZnLuuC/gRMNs 0Vi6lB6P+YRYshVH/EManWXcmrH9kAMEPlBJ6X5LiU0H/yNiBAvKCi/qle+xgFEHwdRtGnfgysHFy n74BLN/XRduaaESDCMQdvZS0QXsIBW/Yk5JtHamyBBZNPIa628bJK18ne3IxyPLnUhtExwdfnC0hr k0gKsOqi9Wo2CxWkMocA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1vn9ma-00000006Ay1-0VO9; Tue, 03 Feb 2026 06:21:04 +0000 Received: from out-188.mta0.migadu.com ([2001:41d0:1004:224b::bc]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1vn9mX-00000006Ax7-0vZT for linux-riscv@lists.infradead.org; Tue, 03 Feb 2026 06:21:03 +0000 Message-ID: <98aef740-fcbe-4d0d-905e-37a7addd5b32@linux.dev> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1770099645; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=GHHv+JYvHw9l6JMKB0V4wzeRAy7p4nA+ckhekF/71zw=; b=sZLk+ciNoUcFLcraK8BqXcqwW4aXkEc5Nsa3XlhYaJk3Kap9n5rkWy1oWGDhzP89DBNpdY ZRm0X+sE0Y3SUMKuw/V6A7uidK20vOGHqQxXjT4JSjjb0jKk57h5IN5Pjf+PMWqPSV6IVa Cnhuz2s3AvHfmo4ex8kXZT+pgGRAbBU= Date: Tue, 3 Feb 2026 14:20:34 +0800 MIME-Version: 1.0 Subject: Re: [PATCH bpf-next 2/3] bpf, riscv: add fsession support for trampolines Content-Language: en-US To: Menglong Dong , ast@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, bjorn@kernel.org, pulehui@huawei.com, puranjay@kernel.org, pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, jiang.biao@linux.dev, bpf@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org References: <20260203055231.1088479-1-dongml2@chinatelecom.cn> <20260203055231.1088479-3-dongml2@chinatelecom.cn> X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Leon Hwang In-Reply-To: <20260203055231.1088479-3-dongml2@chinatelecom.cn> X-Migadu-Flow: FLOW_OUT X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20260202_222102_305612_9EDF64F3 X-CRM114-Status: GOOD ( 22.03 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org On 3/2/26 13:52, Menglong Dong wrote: > 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 > --- > 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_comp64.c > index e4f45e2e7e2f..f10418ff6a9b 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, int args_off, int retval_of > return ret; > } > > +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 = (cookie_off - args_off) / 8; > + > + for (i = 0; i < tl->nr_links; i++) { > + int err; > + > + if (tl->links[i]->link.prog->call_session_cookie) { NIT: we have helper bpf_prog_calls_session_cookie() to read call_session_cookie. Thanks, Leon > + u64 meta = func_meta | ((u64)cur_cookie << BPF_TRAMP_COOKIE_INDEX_SHIFT); > + > + emit_store_stack_imm64(RV_REG_T1, -func_meta_off, meta, ctx); > + cur_cookie--; > + } > + err = 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 = NULL; > int stack_size = 0, nr_arg_slots = 0; > - int retval_off, args_off, nregs_off, ip_off, run_ctx_off, sreg_off, stk_arg_off; > + int retval_off, args_off, func_meta_off, ip_off, run_ctx_off, sreg_off, stk_arg_off; > + int cookie_off, cookie_cnt; > struct bpf_tramp_links *fentry = &tlinks[BPF_TRAMP_FENTRY]; > struct bpf_tramp_links *fexit = &tlinks[BPF_TRAMP_FEXIT]; > struct bpf_tramp_links *fmod_ret = &tlinks[BPF_TRAMP_MODIFY_RETURN]; > bool is_struct_ops = flags & BPF_TRAMP_F_INDIRECT; > void *orig_call = func_addr; > bool save_ret; > + u64 func_meta; > u32 insn; > > /* 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 ] always > * > * 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 += nr_arg_slots * 8; > args_off = stack_size; > > + /* function metadata, such as regs count */ > stack_size += 8; > - nregs_off = stack_size; > + func_meta_off = stack_size; > > if (flags & BPF_TRAMP_F_IP_ARG) { > stack_size += 8; > ip_off = stack_size; > } > > + cookie_cnt = bpf_fsession_cookie_cnt(tlinks); > + /* room for session cookies */ > + stack_size += cookie_cnt * 8; > + cookie_off = stack_size; > + > stack_size += round_up(sizeof(struct bpf_tramp_run_ctx), 8); > run_ctx_off = stack_size; > > @@ -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); > > - emit_store_stack_imm64(RV_REG_T1, -nregs_off, nr_arg_slots, ctx); > + func_meta = nr_arg_slots; > + emit_store_stack_imm64(RV_REG_T1, -func_meta_off, func_meta, ctx); > > store_args(nr_arg_slots, args_off, ctx); > > + if (bpf_fsession_cnt(tlinks)) { > + /* clear all session cookies' value */ > + for (i = 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 = emit_call((const u64)__bpf_tramp_enter, true, ctx); > @@ -1143,9 +1187,9 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, > return ret; > } > > - for (i = 0; i < fentry->nr_links; i++) { > - ret = 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 = 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]) = insn; > } > > - for (i = 0; i < fexit->nr_links; i++) { > - ret = invoke_bpf_prog(fexit->links[i], args_off, retval_off, > - run_ctx_off, false, ctx); > + /* set "is_return" flag for fsession */ > + func_meta |= (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 = invoke_bpf(fexit, args_off, retval_off, run_ctx_off, func_meta_off, > + 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; > +} _______________________________________________ linux-riscv mailing list linux-riscv@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-riscv