mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Chenghao Duan <duanchenghao@kylinos.cn>
To: Hengqi Chen <hengqi.chen@gmail.com>
Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org,
	yangtiezhu@loongson.cn, chenhuacai@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, kernel@xen0n.name,
	linux-kernel@vger.kernel.org, loongarch@lists.linux.dev,
	bpf@vger.kernel.org, guodongtai@kylinos.cn,
	youling.tang@linux.dev, jianghaoran@kylinos.cn,
	vincent.mc.li@gmail.com
Subject: Re: [PATCH v4 5/5] LoongArch: BPF: Add struct ops support for trampoline
Date: Mon, 28 Jul 2025 21:34:18 +0800	[thread overview]
Message-ID: <20250728133418.GC1439240@chenghao-pc> (raw)
In-Reply-To: <CAEyhmHQKBQbidX_SpUF1ZPv7vkkhSR_UuRvxznyb6y5GYQS3qw@mail.gmail.com>

On Mon, Jul 28, 2025 at 06:55:52PM +0800, Hengqi Chen wrote:
> On Thu, Jul 24, 2025 at 10:22 PM Chenghao Duan <duanchenghao@kylinos.cn> wrote:
> >
> > From: Tiezhu Yang <yangtiezhu@loongson.cn>
> >
> > Use BPF_TRAMP_F_INDIRECT flag to detect struct ops and emit proper
> > prologue and epilogue for this case.
> >
> > With this patch, all of the struct_ops related testcases (except
> > struct_ops_multi_pages) passed on LoongArch.
> >
> > The testcase struct_ops_multi_pages failed is because the actual
> > image_pages_cnt is 40 which is bigger than MAX_TRAMP_IMAGE_PAGES.
> >
> > Before:
> >
> >   $ sudo ./test_progs -t struct_ops -d struct_ops_multi_pages
> >   ...
> >   WATCHDOG: test case struct_ops_module/struct_ops_load executes for 10 seconds...
> >
> > After:
> >
> >   $ sudo ./test_progs -t struct_ops -d struct_ops_multi_pages
> >   ...
> >   #15      bad_struct_ops:OK
> >   ...
> >   #399     struct_ops_autocreate:OK
> >   ...
> >   #400     struct_ops_kptr_return:OK
> >   ...
> >   #401     struct_ops_maybe_null:OK
> >   ...
> >   #402     struct_ops_module:OK
> >   ...
> >   #404     struct_ops_no_cfi:OK
> >   ...
> >   #405     struct_ops_private_stack:SKIP
> >   ...
> >   #406     struct_ops_refcounted:OK
> >   Summary: 8/25 PASSED, 3 SKIPPED, 0 FAILED
> >
> > Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
> > ---
> >  arch/loongarch/net/bpf_jit.c | 71 ++++++++++++++++++++++++------------
> >  1 file changed, 47 insertions(+), 24 deletions(-)
> >
> > diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c
> > index ac5ce3a28..6a84fb104 100644
> > --- a/arch/loongarch/net/bpf_jit.c
> > +++ b/arch/loongarch/net/bpf_jit.c
> > @@ -1603,6 +1603,7 @@ static int __arch_prepare_bpf_trampoline(struct jit_ctx *ctx, struct bpf_tramp_i
> >         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;
> >         int ret, save_ret;
> >         void *orig_call = func_addr;
> >         u32 **branches = NULL;
> > @@ -1678,18 +1679,31 @@ static int __arch_prepare_bpf_trampoline(struct jit_ctx *ctx, struct bpf_tramp_i
> >
> >         stack_size = round_up(stack_size, 16);
> >
> > -       /* For the trampoline called from function entry */
> > -       /* RA and FP for parent function*/
> > -       emit_insn(ctx, addid, LOONGARCH_GPR_SP, LOONGARCH_GPR_SP, -16);
> > -       emit_insn(ctx, std, LOONGARCH_GPR_RA, LOONGARCH_GPR_SP, 8);
> > -       emit_insn(ctx, std, LOONGARCH_GPR_FP, LOONGARCH_GPR_SP, 0);
> > -       emit_insn(ctx, addid, LOONGARCH_GPR_FP, LOONGARCH_GPR_SP, 16);
> > -
> > -       /* RA and FP for traced function*/
> > -       emit_insn(ctx, addid, LOONGARCH_GPR_SP, LOONGARCH_GPR_SP, -stack_size);
> > -       emit_insn(ctx, std, LOONGARCH_GPR_T0, LOONGARCH_GPR_SP, stack_size - 8);
> > -       emit_insn(ctx, std, LOONGARCH_GPR_FP, LOONGARCH_GPR_SP, stack_size - 16);
> > -       emit_insn(ctx, addid, LOONGARCH_GPR_FP, LOONGARCH_GPR_SP, stack_size);
> > +       if (!is_struct_ops) {
> > +               /*
> > +                * For the trampoline called from function entry,
> > +                * the frame of traced function and the frame of
> > +                * trampoline need to be considered.
> > +                */
> > +               emit_insn(ctx, addid, LOONGARCH_GPR_SP, LOONGARCH_GPR_SP, -16);
> > +               emit_insn(ctx, std, LOONGARCH_GPR_RA, LOONGARCH_GPR_SP, 8);
> > +               emit_insn(ctx, std, LOONGARCH_GPR_FP, LOONGARCH_GPR_SP, 0);
> > +               emit_insn(ctx, addid, LOONGARCH_GPR_FP, LOONGARCH_GPR_SP, 16);
> > +
> > +               emit_insn(ctx, addid, LOONGARCH_GPR_SP, LOONGARCH_GPR_SP, -stack_size);
> > +               emit_insn(ctx, std, LOONGARCH_GPR_T0, LOONGARCH_GPR_SP, stack_size - 8);
> > +               emit_insn(ctx, std, LOONGARCH_GPR_FP, LOONGARCH_GPR_SP, stack_size - 16);
> > +               emit_insn(ctx, addid, LOONGARCH_GPR_FP, LOONGARCH_GPR_SP, stack_size);
> > +       } else {
> > +               /*
> > +                * For the trampoline called directly, just handle
> > +                * the frame of trampoline.
> > +                */
> > +               emit_insn(ctx, addid, LOONGARCH_GPR_SP, LOONGARCH_GPR_SP, -stack_size);
> > +               emit_insn(ctx, std, LOONGARCH_GPR_RA, LOONGARCH_GPR_SP, stack_size - 8);
> > +               emit_insn(ctx, std, LOONGARCH_GPR_FP, LOONGARCH_GPR_SP, stack_size - 16);
> > +               emit_insn(ctx, addid, LOONGARCH_GPR_FP, LOONGARCH_GPR_SP, stack_size);
> > +       }
> >
> 
> The diff removes code added in patch 4/5, this should be squashed to
> the trampoline patch if possible.

This patch was provided by Tiezhu Yang, and there was a discussion about
it at the time.
https://lore.kernel.org/all/cd190c8a-a7b9-53de-d363-c3d695fe3191@loongson.cn/

> 
> >         /* callee saved register S1 to pass start time */
> >         emit_insn(ctx, std, LOONGARCH_GPR_S1, LOONGARCH_GPR_FP, -sreg_off);
> > @@ -1779,21 +1793,30 @@ static int __arch_prepare_bpf_trampoline(struct jit_ctx *ctx, struct bpf_tramp_i
> >
> >         emit_insn(ctx, ldd, LOONGARCH_GPR_S1, LOONGARCH_GPR_FP, -sreg_off);
> >
> > -       /* trampoline called from function entry */
> > -       emit_insn(ctx, ldd, LOONGARCH_GPR_T0, LOONGARCH_GPR_SP, stack_size - 8);
> > -       emit_insn(ctx, ldd, LOONGARCH_GPR_FP, LOONGARCH_GPR_SP, stack_size - 16);
> > -       emit_insn(ctx, addid, LOONGARCH_GPR_SP, LOONGARCH_GPR_SP, stack_size);
> > +       if (!is_struct_ops) {
> > +               /* trampoline called from function entry */
> > +               emit_insn(ctx, ldd, LOONGARCH_GPR_T0, LOONGARCH_GPR_SP, stack_size - 8);
> > +               emit_insn(ctx, ldd, LOONGARCH_GPR_FP, LOONGARCH_GPR_SP, stack_size - 16);
> > +               emit_insn(ctx, addid, LOONGARCH_GPR_SP, LOONGARCH_GPR_SP, stack_size);
> > +
> > +               emit_insn(ctx, ldd, LOONGARCH_GPR_RA, LOONGARCH_GPR_SP, 8);
> > +               emit_insn(ctx, ldd, LOONGARCH_GPR_FP, LOONGARCH_GPR_SP, 0);
> > +               emit_insn(ctx, addid, LOONGARCH_GPR_SP, LOONGARCH_GPR_SP, 16);
> >
> > -       emit_insn(ctx, ldd, LOONGARCH_GPR_RA, LOONGARCH_GPR_SP, 8);
> > -       emit_insn(ctx, ldd, LOONGARCH_GPR_FP, LOONGARCH_GPR_SP, 0);
> > -       emit_insn(ctx, addid, LOONGARCH_GPR_SP, LOONGARCH_GPR_SP, 16);
> > +               if (flags & BPF_TRAMP_F_SKIP_FRAME)
> > +                       /* return to parent function */
> > +                       emit_insn(ctx, jirl, LOONGARCH_GPR_ZERO, LOONGARCH_GPR_RA, 0);
> > +               else
> > +                       /* return to traced function */
> > +                       emit_insn(ctx, jirl, LOONGARCH_GPR_ZERO, LOONGARCH_GPR_T0, 0);
> > +       } else {
> > +               /* trampoline called directly */
> > +               emit_insn(ctx, ldd, LOONGARCH_GPR_RA, LOONGARCH_GPR_SP, stack_size - 8);
> > +               emit_insn(ctx, ldd, LOONGARCH_GPR_FP, LOONGARCH_GPR_SP, stack_size - 16);
> > +               emit_insn(ctx, addid, LOONGARCH_GPR_SP, LOONGARCH_GPR_SP, stack_size);
> >
> > -       if (flags & BPF_TRAMP_F_SKIP_FRAME)
> > -               /* return to parent function */
> >                 emit_insn(ctx, jirl, LOONGARCH_GPR_ZERO, LOONGARCH_GPR_RA, 0);
> > -       else
> > -               /* return to traced function */
> > -               emit_insn(ctx, jirl, LOONGARCH_GPR_ZERO, LOONGARCH_GPR_T0, 0);
> > +       }
> >
> >         ret = ctx->idx;
> >  out:
> > --
> > 2.25.1
> >

  reply	other threads:[~2025-07-28 13:34 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-24 14:19 [PATCH v4 0/5] Support trampoline for LoongArch Chenghao Duan
2025-07-24 14:19 ` [PATCH v4 1/5] LoongArch: Add larch_insn_gen_{beq,bne} helpers Chenghao Duan
2025-07-24 14:19 ` [PATCH v4 2/5] LoongArch: BPF: Update the code to rename validate_code to validate_ctx Chenghao Duan
2025-07-24 14:19 ` [PATCH v4 3/5] LoongArch: BPF: Add bpf_arch_xxxxx support for Loongarch Chenghao Duan
2025-07-28  2:30   ` Huacai Chen
2025-07-28 10:47   ` Hengqi Chen
2025-07-28 13:21     ` Chenghao Duan
2025-07-29 11:56       ` Chenghao Duan
2025-07-28 10:58   ` Hengqi Chen
2025-07-28 12:59     ` Chenghao Duan
2025-07-24 14:19 ` [PATCH v4 4/5] LoongArch: BPF: Add bpf trampoline " Chenghao Duan
2025-07-28  2:03   ` Geliang Tang
2025-07-28 10:50   ` Hengqi Chen
2025-07-24 14:19 ` [PATCH v4 5/5] LoongArch: BPF: Add struct ops support for trampoline Chenghao Duan
2025-07-28 10:55   ` Hengqi Chen
2025-07-28 13:34     ` Chenghao Duan [this message]
2025-07-29  2:32       ` Huacai Chen
2025-07-24 15:30 ` [PATCH v4 0/5] Support trampoline for LoongArch Vincent Li
2025-07-25 10:18   ` Chenghao Duan
2025-07-26 19:14     ` Daniel Borkmann
2025-07-27  1:00 ` Geliang Tang
2025-07-28  2:42   ` Huacai Chen

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=20250728133418.GC1439240@chenghao-pc \
    --to=duanchenghao@kylinos.cn \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=chenhuacai@kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=guodongtai@kylinos.cn \
    --cc=haoluo@google.com \
    --cc=hengqi.chen@gmail.com \
    --cc=jianghaoran@kylinos.cn \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kernel@xen0n.name \
    --cc=kpsingh@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=loongarch@lists.linux.dev \
    --cc=martin.lau@linux.dev \
    --cc=sdf@fomichev.me \
    --cc=song@kernel.org \
    --cc=vincent.mc.li@gmail.com \
    --cc=yangtiezhu@loongson.cn \
    --cc=yonghong.song@linux.dev \
    --cc=youling.tang@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

all inboxes | Powered by JetHome®