public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
From: Menglong Dong <menglong.dong@linux.dev>
To: Menglong Dong <menglong8.dong@gmail.com>,
	ast@kernel.org, Leon Hwang <leon.hwang@linux.dev>
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
Subject: Re: [PATCH bpf-next 2/3] bpf, riscv: add fsession support for trampolines
Date: Tue, 03 Feb 2026 14:25:12 +0800	[thread overview]
Message-ID: <2816538.mvXUDI8C0e@7940hx> (raw)
In-Reply-To: <98aef740-fcbe-4d0d-905e-37a7addd5b32@linux.dev>

On 2026/2/3 14:20 Leon Hwang <leon.hwang@linux.dev> write:
> 
> 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 <dongml2@chinatelecom.cn>
> > ---
> >  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.

Ah, right, I forget that helper. I'll update it and send the V2 after
getting more feedback.

Thanks!
Menglong Dong

> 
> 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;
> > +}
> 
> 
> 





  reply	other threads:[~2026-02-03  6:25 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-03  5:52 [PATCH bpf-next 0/3] bpf: fsession support for riscv Menglong Dong
2026-02-03  5:52 ` [PATCH bpf-next 1/3] bpf, riscv: introduce emit_store_stack_imm64() for trampoline Menglong Dong
2026-02-03  5:52 ` [PATCH bpf-next 2/3] bpf, riscv: add fsession support for trampolines Menglong Dong
2026-02-03  6:20   ` Leon Hwang
2026-02-03  6:25     ` Menglong Dong [this message]
2026-02-06 11:22   ` Björn Töpel
2026-02-06 12:07     ` Menglong Dong
2026-02-03  5:52 ` [PATCH bpf-next 3/3] selftests/bpf: enable fsession_test on riscv64 Menglong Dong
2026-02-06 11:20 ` [PATCH bpf-next 0/3] bpf: fsession support for riscv Björn Töpel
2026-02-06 11:29   ` Menglong Dong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2816538.mvXUDI8C0e@7940hx \
    --to=menglong.dong@linux.dev \
    --cc=alex@ghiti.fr \
    --cc=andrii@kernel.org \
    --cc=aou@eecs.berkeley.edu \
    --cc=ast@kernel.org \
    --cc=bjorn@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=haoluo@google.com \
    --cc=jiang.biao@linux.dev \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=leon.hwang@linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=martin.lau@linux.dev \
    --cc=menglong8.dong@gmail.com \
    --cc=palmer@dabbelt.com \
    --cc=pjw@kernel.org \
    --cc=pulehui@huawei.com \
    --cc=puranjay@kernel.org \
    --cc=sdf@fomichev.me \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox