BPF Archive mirror
 help / color / mirror / Atom feed
From: Puranjay Mohan <puranjay@kernel.org>
To: Catalin Marinas <catalin.marinas@arm.com>,
	Will Deacon <will@kernel.org>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	Eduard Zingerman <eddyz87@gmail.com>, Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@kernel.org>,
	Stanislav Fomichev <sdf@google.com>, Hao Luo <haoluo@google.com>,
	Jiri Olsa <jolsa@kernel.org>, Zi Shen Lim <zlim.lnx@gmail.com>,
	Xu Kuohai <xukuohai@huawei.com>,
	Florent Revest <revest@chromium.org>,
	linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org, bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next v4 2/2] bpf, arm64: inline bpf_get_smp_processor_id() helper
Date: Tue, 30 Apr 2024 18:48:00 +0000	[thread overview]
Message-ID: <mb61pzfta1ycf.fsf@kernel.org> (raw)
In-Reply-To: <20240429131647.50165-3-puranjay@kernel.org>

Puranjay Mohan <puranjay@kernel.org> writes:

> As ARM64 JIT now implements BPF_MOV64_PERCPU_REG instruction, inline
> bpf_get_smp_processor_id().
>
> ARM64 uses the per-cpu variable cpu_number to store the cpu id.

While implementing this on the RISC-V JIT[1], I realized that reading the
cpu_number from the per-cpu variable is not the best way to do this on
arm64.

arm64 now has the cpu number in the thread_info and reading that would
be more efficient.

Implementation in this patch is emitting:

; int cpu = bpf_get_smp_processor_id();
mov     x7, #0xffff8000ffffffff
movk    x7, #0x8207, lsl #16   
movk    x7, #0x2008            
mrs     x10, tpidr_el1         
add     x7, x7, x10            
ldr     w7, [x7]               

If we do this in the JIT like I did for RISC-V[1]
We can emit:

; int cpu = bpf_get_smp_processor_id();
mrs     x10, sp_el0
ldr     w7, [x10, #24]

This gives ~ 10% improvement on glob-arr-inc and hash-inc compared to
3-4 % which this patch is providing. 

I will send v5 using this approach.

[1] https://lore.kernel.org/all/20240430175834.33152-3-puranjay@kernel.org/

> Here is how the BPF and ARM64 JITed assembly changes after this commit:
>
>                                          BPF
>          		                =====
>               BEFORE                                       AFTER
>              --------                                     -------
>
> int cpu = bpf_get_smp_processor_id();           int cpu = bpf_get_smp_processor_id();
> (85) call bpf_get_smp_processor_id#229032       (18) r0 = 0xffff800082072008
>                                                 (bf) r0 = &(void __percpu *)(r0)
>                                                 (61) r0 = *(u32 *)(r0 +0)
>
> 				      ARM64 JIT
> 				     ===========
>
>               BEFORE                                       AFTER
>              --------                                     -------
>
> int cpu = bpf_get_smp_processor_id();           int cpu = bpf_get_smp_processor_id();
> mov     x10, #0xfffffffffffff4d0                mov     x7, #0xffff8000ffffffff
> movk    x10, #0x802b, lsl #16                   movk    x7, #0x8207, lsl #16
> movk    x10, #0x8000, lsl #32                   movk    x7, #0x2008
> blr     x10                                     mrs     x10, tpidr_el1
> add     x7, x0, #0x0                            add     x7, x7, x10
>                                                 ldr     w7, [x7]
>
> Performance improvement using benchmark[1]
>
>              BEFORE                                       AFTER
>             --------                                     -------
>
> glob-arr-inc   :   23.817 ± 0.019M/s      glob-arr-inc   :   24.631 ± 0.027M/s [+ 3.41%]
> arr-inc        :   23.253 ± 0.019M/s      arr-inc        :   23.742 ± 0.023M/s [+ 2.10%]
> hash-inc       :   12.258 ± 0.010M/s      hash-inc       :   12.625 ± 0.004M/s [+ 3.00%]
>
> [1] https://github.com/anakryiko/linux/commit/8dec900975ef


Thanks,
Puranjay

      reply	other threads:[~2024-04-30 18:48 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-29 13:16 [PATCH bpf-next v4 0/2] bpf, arm64: Support per-cpu instruction Puranjay Mohan
2024-04-29 13:16 ` [PATCH bpf-next v4 1/2] arm64, bpf: add internal-only MOV instruction to resolve per-CPU addrs Puranjay Mohan
2024-04-29 13:16 ` [PATCH bpf-next v4 2/2] bpf, arm64: inline bpf_get_smp_processor_id() helper Puranjay Mohan
2024-04-30 18:48   ` Puranjay Mohan [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=mb61pzfta1ycf.fsf@kernel.org \
    --to=puranjay@kernel.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=catalin.marinas@arm.com \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=revest@chromium.org \
    --cc=sdf@google.com \
    --cc=song@kernel.org \
    --cc=will@kernel.org \
    --cc=xukuohai@huawei.com \
    --cc=yonghong.song@linux.dev \
    --cc=zlim.lnx@gmail.com \
    /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).