BPF List
 help / color / mirror / Atom feed
From: Puranjay Mohan <puranjay@kernel.org>
To: "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@vger.kernel.org
Subject: Re: On inlining more helpers in the JITs or the verifier
Date: Thu, 02 May 2024 19:19:49 +0000	[thread overview]
Message-ID: <mb61pzft8vx62.fsf@kernel.org> (raw)
In-Reply-To: <mb61p1q6k869u.fsf@kernel.org>

Puranjay Mohan <puranjay@kernel.org> writes:

> 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()

Tried inlining bpf_get_current_task() on ARM64:

                  Before                                                                  After
                 --------                                                               --------

bpf_prog_6e2672bcc4451a42_trigger_get_current_task:                      bpf_prog_6e2672bcc4451a42_trigger_get_current_task:
; task = (struct task_struct *)bpf_get_current_task();                   ; task = (struct task_struct *)bpf_get_current_task();
  34:   mov     x10, #0xffffffffffff9838                                   34:   mrs     x7, sp_el0
  38:   movk    x10, #0x8027, lsl #16
  3c:   movk    x10, #0x8000, lsl #32
  40:   blr     x10
  44:   add     x7, x0, #0x0



In the non-inlined version there is a branch [blr x10] to:

0xffff800080279838 bpf_get_current_task:
                     <+0>:     mrs     x0, sp_el0
                     <+4>:     ret


So, we only need a single instruction after inlining!!

I just don't know the best way to benchmark this. In theory it looks
highly optimized.

Thanks,
Puranjay

  reply	other threads:[~2024-05-02 19:19 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 [this message]
2024-05-02 21:22 ` Andrii Nakryiko
2024-05-03 16:04   ` Alexei Starovoitov

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=mb61pzft8vx62.fsf@kernel.org \
    --to=puranjay@kernel.org \
    --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=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