bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: <puranjay@kernel.org>
To: Xu Kuohai <xukuohai@huaweicloud.com>,
	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@fomichev.me>, Hao Luo <haoluo@google.com>,
	Jiri Olsa <jolsa@kernel.org>,
	Catalin Marinas <catalin.marinas@arm.com>,
	Will Deacon <will@kernel.org>, Mykola Lysenko <mykolal@fb.com>,
	Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next 1/2] bpf, arm64: Add JIT support for timed may_goto
Date: Thu, 07 Aug 2025 13:27:34 +0000	[thread overview]
Message-ID: <mb61pectngt2x.fsf@kernel.org> (raw)
In-Reply-To: <ecf88f6e-1941-4278-815c-e003dd7b9621@huaweicloud.com>

Xu Kuohai <xukuohai@huaweicloud.com> writes:

> On 7/24/2025 8:54 PM, Puranjay Mohan wrote:
>> When verifier sees a timed may_goto instruction, it emits a call to
>> arch_bpf_timed_may_goto() with a stack offset in BPF_REG_AX (arm64 r9)
>> and expects a count value to be returned in the same register. The
>> verifier doesn't save or restore any registers before emitting this
>> call.
>> 
>> arch_bpf_timed_may_goto() should act as a trampoline to call
>> bpf_check_timed_may_goto() with AAPCS64 calling convention.
>> 
>> To support this custom calling convention, implement
>> arch_bpf_timed_may_goto() in assembly and make sure BPF caller saved
>> registers are saved and restored, call bpf_check_timed_may_goto with
>> arm64 calling convention where first argument and return value both are
>> in x0, then put the result back into BPF_REG_AX before returning.
>> 
>> Signed-off-by: Puranjay Mohan <puranjay@kernel.org>
>> ---
>>   arch/arm64/net/Makefile             |  2 +-
>>   arch/arm64/net/bpf_jit_comp.c       | 13 ++++++++++-
>>   arch/arm64/net/bpf_timed_may_goto.S | 36 +++++++++++++++++++++++++++++
>>   3 files changed, 49 insertions(+), 2 deletions(-)
>>   create mode 100644 arch/arm64/net/bpf_timed_may_goto.S
>> 
>> diff --git a/arch/arm64/net/Makefile b/arch/arm64/net/Makefile
>> index 5c540efb7d9b9..3ae382bfca879 100644
>> --- a/arch/arm64/net/Makefile
>> +++ b/arch/arm64/net/Makefile
>> @@ -2,4 +2,4 @@
>>   #
>>   # ARM64 networking code
>>   #
>> -obj-$(CONFIG_BPF_JIT) += bpf_jit_comp.o
>> +obj-$(CONFIG_BPF_JIT) += bpf_jit_comp.o bpf_timed_may_goto.o
>> diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c
>> index 89b1b8c248c62..6c954b36f57ea 100644
>> --- a/arch/arm64/net/bpf_jit_comp.c
>> +++ b/arch/arm64/net/bpf_jit_comp.c
>> @@ -1505,7 +1505,13 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
>>   		if (ret < 0)
>>   			return ret;
>>   		emit_call(func_addr, ctx);
>> -		emit(A64_MOV(1, r0, A64_R(0)), ctx);
>> +		/*
>> +		 * Call to arch_bpf_timed_may_goto() is emitted by the
>> +		 * verifier and called with custom calling convention with
>> +		 * first argument and return value in BPF_REG_AX (x9).
>> +		 */
>> +		if (func_addr != (u64)arch_bpf_timed_may_goto)
>> +			emit(A64_MOV(1, r0, A64_R(0)), ctx);
>>   		break;
>>   	}
>>   	/* tail call */
>> @@ -2914,6 +2920,11 @@ bool bpf_jit_bypass_spec_v4(void)
>>   	return true;
>>   }
>>   
>> +bool bpf_jit_supports_timed_may_goto(void)
>> +{
>> +	return true;
>> +}
>> +
>>   bool bpf_jit_inlines_helper_call(s32 imm)
>>   {
>>   	switch (imm) {
>> diff --git a/arch/arm64/net/bpf_timed_may_goto.S b/arch/arm64/net/bpf_timed_may_goto.S
>> new file mode 100644
>> index 0000000000000..45f80e752345c
>> --- /dev/null
>> +++ b/arch/arm64/net/bpf_timed_may_goto.S
>> @@ -0,0 +1,36 @@
>> +/* SPDX-License-Identifier: GPL-2.0 */
>> +/* Copyright (c) 2025 Puranjay Mohan <puranjay@kernel.org> */
>> +
>> +#include <linux/linkage.h>
>> +
>> +SYM_FUNC_START(arch_bpf_timed_may_goto)
>> +	/* Allocate stack space and emit frame record */
>> +	stp     x29, x30, [sp, #-64]!
>> +	mov     x29, sp
>> +
>> +	/* Save BPF registers R0 - R5 (x7, x0-x4)*/
>> +	stp	x7, x0, [sp, #16]
>> +	stp	x1, x2, [sp, #32]
>> +	stp	x3, x4, [sp, #48]
>> +
>> +	/*
>> +	 * Stack depth was passed in BPF_REG_AX (x9), add it to the BPF_FP
>> +	 * (x25) to get the pointer to count and timestamp and pass it as the
>> +	 * first argument in x0.
>> +	 */
>> +	add	x0, x9, x25
>
> Whether BPF_REG_FP (x25) is set up by the arm64 jit depends on whether
> the jit detects any bpf instruction using it. Before generating the
> call to arch_bpf_timed_may_goto, the verifier generates a load
> instruction using FP, i.e. AX = *(u64 *)(FP - stack_off_cnt),
> so FP is always set up in this case.
>
> It seems a bit subtle. Maybe we should add a comment here?

Yes, a comment would be useful. I will add it in the next version.

>> +	bl	bpf_check_timed_may_goto
>> +	/* BPF_REG_AX(x9) will be stored into count, so move return value to it. */
>> +	mov	x9, x0
>> +
>> +
>
> Nit: one extra blank line
>

Thanks,
Puranjay

  reply	other threads:[~2025-08-07 13:27 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-24 12:54 [PATCH bpf-next 0/2] bpf, arm64: support for timed may_goto Puranjay Mohan
2025-07-24 12:54 ` [PATCH bpf-next 1/2] bpf, arm64: Add JIT " Puranjay Mohan
2025-08-05 11:28   ` Xu Kuohai
2025-08-07 13:27     ` puranjay [this message]
2025-07-24 12:54 ` [PATCH bpf-next 2/2] selftests/bpf: Enable timed may_goto tests for arm64 Puranjay Mohan

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=mb61pectngt2x.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=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=mykolal@fb.com \
    --cc=sdf@fomichev.me \
    --cc=song@kernel.org \
    --cc=will@kernel.org \
    --cc=xukuohai@huaweicloud.com \
    --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 NNTP newsgroup(s).