BPF Archive mirror
 help / color / mirror / Atom feed
From: Alexei Starovoitov <alexei.starovoitov@gmail.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: "Puranjay Mohan" <puranjay@kernel.org>,
	"Björn Töpel" <bjorn@kernel.org>,
	"Alexei Starovoitov" <ast@kernel.org>,
	"Daniel Borkmann" <daniel@iogearbox.net>,
	"Andrii Nakryiko" <andrii@kernel.org>,
	"Martin KaFai Lau" <martin.lau@linux.dev>,
	"Song Liu" <song@kernel.org>,
	"Yonghong Song" <yonghong.song@linux.dev>,
	"John Fastabend" <john.fastabend@gmail.com>,
	"Kumar Kartikeya Dwivedi" <memxor@gmail.com>,
	bpf <bpf@vger.kernel.org>
Subject: Re: On inlining more helpers in the JITs or the verifier
Date: Fri, 3 May 2024 09:04:49 -0700	[thread overview]
Message-ID: <CAADnVQ+rLCPBKNgSerzS8DKbdHEDgDyqUAjOYA-rP36oXwnY7A@mail.gmail.com> (raw)
In-Reply-To: <CAEf4BzYPQ7QS+UUxYj=okGyKirkqkmT+48DiRQFZ_DeC+zo+cA@mail.gmail.com>

On Thu, May 2, 2024 at 2:22 PM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Thu, May 2, 2024 at 10:37 AM Puranjay Mohan <puranjay@kernel.org> wrote:
> >
> >
> > Hi Everyone,
> >
> > While working on inlining bpf_get_smp_processor_id() in the ARM64 and
> > RISCV JITs, I realized that these archs allow such optimizations because
> > they keep some information like the per-cpu offset or the pointer to the
> > task_struct in special system registers.
> >
> > So, I went through the list of all BPF helpers and made a list of
> > helpers that we can inline in these JITs to make their usage much more
> > optimized:
> >
> > I. ARM64 and RISC-V specific optimzations if inlined:
> >
> >     A) Because pointer to tast_struct is available in a register:
> >         1. bpf_get_current_pid_tgid()
> >         2. bpf_get_current_task()
>
> These two are used really frequently, so it might make sense to
> optimize them (and also bpf_get_current_task_btf(), of course), if
> others agree with me.
>
> >         3. bpf_set_retval()
> >         4. bpf_get_retval()
> >         5. bpf_task_pt_regs()
>
> I'm leaning towards saying that probably not, unless we have a really
> good reason to. Inlining is not free in terms of code maintenance and
> complexity, so I wouldn't go and inline everything possible. But maybe
> others have another opinion.
>
>
> >         6. bpf_get_attach_cookie()
>
> definitely no, there are multiple implementations depending on
> specific program type
>
> >
> >     B) Because per_cpu offset is available in a register:
> >         1. bpf_this_cpu_ptr()
>
> maybe, but I don't think we inline at BPF instruction level, so
> inlining in BPF JIT seems premature
>
>
> >         2. bpf_get_numa_node_id()
>
> I'm not sure how actively this is used, so I'd say no to this one as well.
>
> >
> >         These can be inlined in the verifier too using the newly
> >         introduced per-cpu instruction.
>
> yep, I'd start with doing BPF assembly inlining for
> bpf_this_cpu_ptr/bpf_per_cpu_ptr, tbh
>
> >
> > II. These are very basic writes, can be inlined in the verifier or the JIT:
> >     1. bpf_msg_apply_bytes()
> >     2. bpf_msg_cork_bytes()
> >     3. bpf_set_hash_invalid()
>
> I'd say this is also going overboard with inlining.

+1

simplicity of logic is not a reason to inline it.
I would only inline bpf_get_current_task[_btf]() and do it
in the verifier. JITs should inline only if perf delta
is really significant.
I hope bpf_get_smp_processor_id() will be the only such example.

      reply	other threads:[~2024-05-03 16:05 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-02 17:37 On inlining more helpers in the JITs or the verifier Puranjay Mohan
2024-05-02 19:19 ` Puranjay Mohan
2024-05-02 21:22 ` Andrii Nakryiko
2024-05-03 16:04   ` Alexei Starovoitov [this message]

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=CAADnVQ+rLCPBKNgSerzS8DKbdHEDgDyqUAjOYA-rP36oXwnY7A@mail.gmail.com \
    --to=alexei.starovoitov@gmail.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bjorn@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=john.fastabend@gmail.com \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=puranjay@kernel.org \
    --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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).