All of lore.kernel.org
 help / color / mirror / Atom feed
From: Puranjay Mohan <puranjay@kernel.org>
To: Xu Kuohai <xukuohai@huaweicloud.com>, bpf@vger.kernel.org
Cc: 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>,
	Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	Mark Rutland <mark.rutland@arm.com>,
	Will Deacon <will@kernel.org>,
	Catalin Marinas <catalin.marinas@arm.com>,
	Tejun Heo <tj@kernel.org>, Puranjay Mohan <puranjay12@gmail.com>
Subject: Re: [PATCH bpf-next 2/7] arm64: insn: Add encoder for ADD/SUB (extended register)
Date: Thu, 13 Aug 2026 20:21:15 +0100	[thread overview]
Message-ID: <m2jyptzv10.fsf@kernel.org> (raw)
In-Reply-To: <e15065e4-881c-433a-9bc5-abd9991b0714@huaweicloud.com>

Xu Kuohai <xukuohai@huaweicloud.com> writes:

> On 8/11/2026 3:09 AM, Puranjay Mohan wrote:
>> From: Tejun Heo <tj@kernel.org>
>> 
>> The insn library encodes the immediate and shifted-register forms of
>> ADD/SUB but not the extended-register form. The BPF JIT wants it to
>> rebase a 32-bit arena offset onto the arena kernel base in a single
>> instruction, add xN, xBASE, wN, uxtw, instead of a separate zero-extend
>> followed by a plain add.
>> 
>> Add aarch64_insn_gen_add_sub_extended_reg(), modeled on the
>> shifted-register generator. The option and imm3 fields occupy the same
>> bits as the shifted form's shift amount, so they are encoded through the
>> existing IMM_6 field type.
>> 
>> Note that register 31 does not mean the same thing in the two forms: in
>> the extended-register encoding it is SP for Rn, and for Rd unless the
>> instruction sets the flags, while it stays XZR for Rm. Callers porting a
>> shifted-register site that passes A64_ZR need to be aware of that, so
>> say so above the function.
>> 
>> Signed-off-by: Tejun Heo <tj@kernel.org>
>> Signed-off-by: Puranjay Mohan <puranjay@kernel.org>
>> ---
>>   arch/arm64/include/asm/insn.h | 23 ++++++++++++++
>>   arch/arm64/lib/insn.c         | 60 +++++++++++++++++++++++++++++++++++
>>   2 files changed, 83 insertions(+)
>> 
>> diff --git a/arch/arm64/include/asm/insn.h b/arch/arm64/include/asm/insn.h
>> index cc0702fa64a79..4548e8015808d 100644
>> --- a/arch/arm64/include/asm/insn.h
>> +++ b/arch/arm64/include/asm/insn.h
>> @@ -205,6 +205,18 @@ enum aarch64_insn_adsb_type {
>>   	AARCH64_INSN_ADSB_SUB_SETFLAGS
>>   };
>>   
>> +/* option field of add/sub (extended register) */
>> +enum aarch64_insn_extend_type {
>> +	AARCH64_INSN_EXTEND_UXTB,
>> +	AARCH64_INSN_EXTEND_UXTH,
>> +	AARCH64_INSN_EXTEND_UXTW,
>> +	AARCH64_INSN_EXTEND_UXTX,
>> +	AARCH64_INSN_EXTEND_SXTB,
>> +	AARCH64_INSN_EXTEND_SXTH,
>> +	AARCH64_INSN_EXTEND_SXTW,
>> +	AARCH64_INSN_EXTEND_SXTX,
>> +};
>> +
>
> I checked the Arm Architecture Reference Manual(ARM DDI 0487 M.c).
> The enum values correctly match the 3-bit "extend" values defined in
> the manual.
>
>>   enum aarch64_insn_movewide_type {
>>   	AARCH64_INSN_MOVEWIDE_ZERO,
>>   	AARCH64_INSN_MOVEWIDE_KEEP,
>> @@ -378,6 +390,10 @@ __AARCH64_INSN_FUNCS(add,	0x7F200000, 0x0B000000)
>>   __AARCH64_INSN_FUNCS(adds,	0x7F200000, 0x2B000000)
>>   __AARCH64_INSN_FUNCS(sub,	0x7F200000, 0x4B000000)
>>   __AARCH64_INSN_FUNCS(subs,	0x7F200000, 0x6B000000)
>> +__AARCH64_INSN_FUNCS(add_ext,	0x7F200000, 0x0B200000)
>> +__AARCH64_INSN_FUNCS(adds_ext,	0x7F200000, 0x2B200000)
>> +__AARCH64_INSN_FUNCS(sub_ext,	0x7F200000, 0x4B200000)
>> +__AARCH64_INSN_FUNCS(subs_ext,	0x7F200000, 0x6B200000)
>
> Page C4-814 of the manual ARM DDI 0487 M.c says the bits 22~23
> are fixed to 0 for ADD/SUB(extended register) instructions, so
> the mask should be 0x7FE00000, not 0x7F200000.

Thanks for catching this, fixed in v2.

>>   __AARCH64_INSN_FUNCS(madd,	0x7FE08000, 0x1B000000)
>>   __AARCH64_INSN_FUNCS(msub,	0x7FE08000, 0x1B008000)
>>   __AARCH64_INSN_FUNCS(udiv,	0x7FE0FC00, 0x1AC00800)
>> @@ -637,6 +653,13 @@ u32 aarch64_insn_gen_add_sub_shifted_reg(enum aarch64_insn_register dst,
>>   					 int shift,
>>   					 enum aarch64_insn_variant variant,
>>   					 enum aarch64_insn_adsb_type type);
>> +u32 aarch64_insn_gen_add_sub_extended_reg(enum aarch64_insn_register dst,
>> +					  enum aarch64_insn_register src,
>> +					  enum aarch64_insn_register reg,
>> +					  enum aarch64_insn_extend_type extend,
>> +					  int shift,
>> +					  enum aarch64_insn_variant variant,
>> +					  enum aarch64_insn_adsb_type type);
>>   u32 aarch64_insn_gen_data1(enum aarch64_insn_register dst,
>>   			   enum aarch64_insn_register src,
>>   			   enum aarch64_insn_variant variant,
>> diff --git a/arch/arm64/lib/insn.c b/arch/arm64/lib/insn.c
>> index 37ce75f7f1f08..e70ac02385153 100644
>> --- a/arch/arm64/lib/insn.c
>> +++ b/arch/arm64/lib/insn.c
>> @@ -986,6 +986,66 @@ u32 aarch64_insn_gen_add_sub_shifted_reg(enum aarch64_insn_register dst,
>>   	return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_6, insn, shift);
>>   }
>>   
>> +/*
>> + * Unlike the shifted-register form, register 31 is not XZR everywhere here:
>> + * it encodes SP for @src, and for @dst too unless @type sets the flags. Only
>> + * @reg keeps the XZR meaning.
>> + */
>> +u32 aarch64_insn_gen_add_sub_extended_reg(enum aarch64_insn_register dst,
>> +					  enum aarch64_insn_register src,
>> +					  enum aarch64_insn_register reg,
>> +					  enum aarch64_insn_extend_type extend,
>> +					  int shift,
>> +					  enum aarch64_insn_variant variant,
>> +					  enum aarch64_insn_adsb_type type)
>> +{
>> +	u32 insn;
>> +
>> +	switch (type) {
>> +	case AARCH64_INSN_ADSB_ADD:
>> +		insn = aarch64_insn_get_add_ext_value();
>> +		break;
>> +	case AARCH64_INSN_ADSB_SUB:
>> +		insn = aarch64_insn_get_sub_ext_value();
>> +		break;
>> +	case AARCH64_INSN_ADSB_ADD_SETFLAGS:
>> +		insn = aarch64_insn_get_adds_ext_value();
>> +		break;
>> +	case AARCH64_INSN_ADSB_SUB_SETFLAGS:
>> +		insn = aarch64_insn_get_subs_ext_value();
>> +		break;
>> +	default:
>> +		pr_err("%s: unknown add/sub encoding %d\n", __func__, type);
>> +		return AARCH64_BREAK_FAULT;
>> +	}
>> +
>> +	switch (variant) {
>> +	case AARCH64_INSN_VARIANT_32BIT:
>> +		break;
>> +	case AARCH64_INSN_VARIANT_64BIT:
>> +		insn |= AARCH64_INSN_SF_BIT;
>> +		break;
>> +	default:
>> +		pr_err("%s: unknown variant encoding %d\n", __func__, variant);
>> +		return AARCH64_BREAK_FAULT;
>> +	}
>> +
>> +	if (shift < 0 || shift > 4) {
>> +		pr_err("%s: invalid shift encoding %d\n", __func__, shift);
>> +		return AARCH64_BREAK_FAULT;
>> +	}
>> +
>> +	insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RD, insn, dst);
>> +
>> +	insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RN, insn, src);
>> +
>> +	insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RM, insn, reg);
>> +
>> +	/* option in bits [15:13] and imm3 in [12:10] together fill IMM_6 */
>> +	return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_6, insn,
>> +					     (extend << 3) | shift);
>> +}
>> +
>>   u32 aarch64_insn_gen_data1(enum aarch64_insn_register dst,
>>   			   enum aarch64_insn_register src,
>>   			   enum aarch64_insn_variant variant,
>
> Except for the mask value mentioned above, the rest lgtm.
>
> Reviewed-by: Xu Kuohai <xukuohai@huawei.com>

Thanks for the review.

  reply	other threads:[~2026-08-13 19:21 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-10 19:09 [PATCH bpf-next 0/7] bpf, arm64: __arena kfunc and struct_ops arguments Puranjay Mohan
2026-08-10 19:09 ` [PATCH bpf-next 1/7] bpf, arm64: Fix stack-passed arguments for indirect trampolines Puranjay Mohan
2026-08-13  9:03   ` Xu Kuohai
2026-08-10 19:09 ` [PATCH bpf-next 2/7] arm64: insn: Add encoder for ADD/SUB (extended register) Puranjay Mohan
2026-08-10 19:19   ` sashiko-bot
2026-08-13 19:28     ` Puranjay Mohan
2026-08-13 10:11   ` Xu Kuohai
2026-08-13 19:21     ` Puranjay Mohan [this message]
2026-08-10 19:09 ` [PATCH bpf-next 3/7] bpf, arm64: JIT __arena kfunc argument rebasing Puranjay Mohan
2026-08-13 11:36   ` Xu Kuohai
2026-08-10 19:09 ` [PATCH bpf-next 4/7] bpf, arm64: Convert struct_ops arena arguments in the trampoline Puranjay Mohan
2026-08-13 11:51   ` Xu Kuohai
2026-08-13 19:20     ` Puranjay Mohan
2026-08-14  2:10       ` Xu Kuohai
2026-08-10 19:09 ` [PATCH bpf-next 5/7] selftests/bpf: Add arm64 JIT-sequence tests for __arena kfunc arguments Puranjay Mohan
2026-08-13 11:56   ` Xu Kuohai
2026-08-10 19:09 ` [PATCH bpf-next 6/7] selftests/bpf: Enable __arena argument tests on arm64 Puranjay Mohan
2026-08-13 11:57   ` Xu Kuohai
2026-08-10 19:09 ` [PATCH bpf-next 7/7] selftests/bpf: Test a multi-slot argument before a struct_ops arena argument Puranjay Mohan
2026-08-13 12:07   ` Xu Kuohai
2026-08-12 23:50 ` [PATCH bpf-next 0/7] bpf, arm64: __arena kfunc and struct_ops arguments Kumar Kartikeya Dwivedi
2026-08-13  2:33   ` Xu Kuohai
2026-08-13  3:32     ` Kumar Kartikeya Dwivedi

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=m2jyptzv10.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=mark.rutland@arm.com \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=puranjay12@gmail.com \
    --cc=song@kernel.org \
    --cc=tj@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.