BPF List
 help / color / mirror / Atom feed
From: Kui-Feng Lee <kuifeng@fb.com>
To: "alexei.starovoitov@gmail.com" <alexei.starovoitov@gmail.com>
Cc: "daniel@iogearbox.net" <daniel@iogearbox.net>,
	"ast@kernel.org" <ast@kernel.org>,
	"andrii@kernel.org" <andrii@kernel.org>,
	"bpf@vger.kernel.org" <bpf@vger.kernel.org>
Subject: Re: [PATCH bpf-next v2 2/4] bpf, x86: Create bpf_trace_run_ctx on the caller thread's stack
Date: Mon, 21 Mar 2022 19:00:38 +0000	[thread overview]
Message-ID: <a3270d0687f90ee9a87ec65b6617da4cbf4e3ca5.camel@fb.com> (raw)
In-Reply-To: <CAADnVQJTyjDM-5Mo_R+B0_gj6tZH5zfP9k1dD48h=Nrc7p8rWA@mail.gmail.com>

On Sun, 2022-03-20 at 13:08 -0700, Alexei Starovoitov wrote:
> On Sun, Mar 20, 2022 at 2:31 AM Kui-Feng Lee <kuifeng@fb.com> wrote:
> > 
> > On Fri, 2022-03-18 at 12:09 -0700, Alexei Starovoitov wrote:
> > > On Tue, Mar 15, 2022 at 05:42:29PM -0700, Kui-Feng Lee wrote:
> > > > BPF trampolines will create a bpf_trace_run_ctx on their
> > > > stacks,
> > > > and
> > > > set/reset the current bpf_run_ctx whenever calling/returning
> > > > from a
> > > > bpf_prog.
> > > > 
> > > > Signed-off-by: Kui-Feng Lee <kuifeng@fb.com>
> > > > ---
> > > >  arch/x86/net/bpf_jit_comp.c | 32
> > > > ++++++++++++++++++++++++++++++++
> > > >  include/linux/bpf.h         | 12 ++++++++----
> > > >  kernel/bpf/syscall.c        |  4 ++--
> > > >  kernel/bpf/trampoline.c     | 21 +++++++++++++++++----
> > > >  4 files changed, 59 insertions(+), 10 deletions(-)
> > > > 
> > > > diff --git a/arch/x86/net/bpf_jit_comp.c
> > > > b/arch/x86/net/bpf_jit_comp.c
> > > > index 1228e6e6a420..29775a475513 100644
> > > > --- a/arch/x86/net/bpf_jit_comp.c
> > > > +++ b/arch/x86/net/bpf_jit_comp.c
> > > > @@ -1748,10 +1748,33 @@ static int invoke_bpf_prog(const struct
> > > > btf_func_model *m, u8 **pprog,
> > > >  {
> > > >         u8 *prog = *pprog;
> > > >         u8 *jmp_insn;
> > > > +       int ctx_cookie_off = offsetof(struct bpf_trace_run_ctx,
> > > > bpf_cookie);
> > > >         struct bpf_prog *p = l->prog;
> > > > 
> > > > +       EMIT1(0x52);             /* push rdx */
> > > 
> > > Why save/restore rdx?
> > 
> > > 
> > > > +
> > > > +       /* mov rdi, 0 */
> > > > +       emit_mov_imm64(&prog, BPF_REG_1, 0, 0);
> > > > +
> > > > +       /* Prepare struct bpf_trace_run_ctx.
> > > > +        * sub rsp, sizeof(struct bpf_trace_run_ctx)
> > > > +        * mov rax, rsp
> > > > +        * mov QWORD PTR [rax + ctx_cookie_off], rdi
> > > > +        */
> > > 
> > > How about the following instead:
> > > sub rsp, sizeof(struct bpf_trace_run_ctx)
> > > mov qword ptr [rsp + ctx_cookie_off], 0
> > > ?
> > 
> > AFAIK, rsp can not be used with the base + displacement addressing
> > mode.  Although, it can be used with base + index + displacement
> > addressing mode.
> 
> Where did you find this?

I use the following document to figure out opcodes.

https://ref.x86asm.net/coder64.html#modrm_byte_32_64

It lists available addressing modes and codes.

By the way, I found I had missed SIB byte, that provides extra
features.  It seems working for this case.  I will try it.


> 
> 0:  48 c7 44 24 08 00 00    mov    QWORD PTR [rsp+0x8],0x0
> 7:  00 00
> 
> > > 
> > > > +       EMIT4(0x48, 0x83, 0xEC, sizeof(struct
> > > > bpf_trace_run_ctx));
> > > > +       EMIT3(0x48, 0x89, 0xE0);
> > > > +       EMIT4(0x48, 0x89, 0x78, ctx_cookie_off);
> > > > +
> > > > +       /* mov rdi, rsp */
> > > > +       EMIT3(0x48, 0x89, 0xE7);
> > > > +       /* mov QWORD PTR [rdi + sizeof(struct
> > > > bpf_trace_run_ctx)],
> > > > rax */
> > > > +       emit_stx(&prog, BPF_DW, BPF_REG_1, BPF_REG_0,
> > > > sizeof(struct
> > > > bpf_trace_run_ctx));
> > > 
> > > why not to do:
> > > mov qword ptr[rsp + sizeof(struct bpf_trace_run_ctx)], rsp
> > > ?
> > 
> > The same reason as above.
> 
> 0:  48 89 64 24 08          mov    QWORD PTR [rsp+0x8],rsp


  reply	other threads:[~2022-03-21 19:00 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-16  0:42 [PATCH bpf-next v2 0/4] Attach a cookie to a tracing program Kui-Feng Lee
2022-03-16  0:42 ` [PATCH bpf-next v2 1/4] bpf, x86: Generate trampolines from bpf_links Kui-Feng Lee
2022-03-16  0:42 ` [PATCH bpf-next v2 2/4] bpf, x86: Create bpf_trace_run_ctx on the caller thread's stack Kui-Feng Lee
2022-03-18 19:09   ` Alexei Starovoitov
2022-03-20  9:31     ` Kui-Feng Lee
2022-03-20 20:08       ` Alexei Starovoitov
2022-03-21 19:00         ` Kui-Feng Lee [this message]
2022-03-21 23:04   ` Andrii Nakryiko
2022-03-21 23:25     ` Alexei Starovoitov
2022-03-21 23:38       ` Andrii Nakryiko
2022-03-21 23:08   ` Andrii Nakryiko
2022-03-22 15:30     ` Kui-Feng Lee
2022-03-22 21:08       ` Andrii Nakryiko
2022-03-16  0:42 ` [PATCH bpf-next v2 3/4] bpf, x86: Support BPF cookie for fentry/fexit/fmod_ret Kui-Feng Lee
2022-03-18 19:13   ` Alexei Starovoitov
2022-03-21 23:24     ` Andrii Nakryiko
2022-03-21 23:37       ` Andrii Nakryiko
2022-04-12 16:50         ` Kui-Feng Lee
2022-03-22  1:15       ` Alexei Starovoitov
2022-03-22  4:32         ` Andrii Nakryiko
2022-04-06  5:35           ` Kui-Feng Lee
2022-04-06 17:00             ` Andrii Nakryiko
2022-03-21 23:18   ` Andrii Nakryiko
2022-03-22 16:08     ` Kui-Feng Lee
2022-03-22 21:06       ` Andrii Nakryiko
2022-04-06 22:44         ` Kui-Feng Lee
2022-03-16  0:42 ` [PATCH bpf-next v2 4/4] selftest/bpf: The test cses of " Kui-Feng Lee
2022-03-18 19:21   ` Alexei Starovoitov
2022-03-20  8:43     ` Kui-Feng Lee
2022-03-21 23:29       ` Andrii Nakryiko
2022-03-21 23:36   ` Andrii Nakryiko

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=a3270d0687f90ee9a87ec65b6617da4cbf4e3ca5.camel@fb.com \
    --to=kuifeng@fb.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    /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