All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eduard Zingerman <eddyz87@gmail.com>
To: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Cc: bpf@vger.kernel.org,  Alexei Starovoitov <ast@kernel.org>,
	 Andrii Nakryiko <andrii@kernel.org>,
	 Daniel Borkmann <daniel@iogearbox.net>,
	Martin KaFai Lau <martin.lau@kernel.org>,
	 kkd@meta.com, kernel-team@meta.com
Subject: Re: [PATCH bpf-next v2] bpf, x86: Add support for signed arena loads
Date: Wed, 14 May 2025 14:13:34 -0700	[thread overview]
Message-ID: <m2y0uyc2v5.fsf@gmail.com> (raw)
In-Reply-To: <20250514175415.2045783-1-memxor@gmail.com> (Kumar Kartikeya Dwivedi's message of "Wed, 14 May 2025 10:54:15 -0700")

Kumar Kartikeya Dwivedi <memxor@gmail.com> writes:

[...]

> +static void emit_ldsx_index(u8 **pprog, u32 size, u32 dst_reg, u32 src_reg, u32 index_reg, int off)
> +{
> +	u8 *prog = *pprog;
> +
> +	switch (size) {
> +	case BPF_B:
> +		/* movsx rax, byte ptr [rax + r12 + off] */
> +		EMIT3(add_3mod(0x40, src_reg, dst_reg, index_reg), 0x0F, 0xBE);
> +		break;
> +	case BPF_H:
> +		/* movsx rax, word ptr [rax + r12 + off] */
> +		EMIT3(add_3mod(0x40, src_reg, dst_reg, index_reg), 0x0F, 0xBF);
> +		break;
> +	case BPF_W:
> +		/* movsx rax, dword ptr [rax + r12 + off] */
> +		EMIT2(add_3mod(0x40, src_reg, dst_reg, index_reg), 0x63);
> +		break;
> +	}
> +	emit_insn_suffix_SIB(&prog, src_reg, dst_reg, index_reg, off);
> +	*pprog = prog;
> +}
> +

I tried the following test to see what disassembly looks like:

  SEC("syscall")
  __success
  __arch_x86_64
  __jited("movslq	0x10(%rax,%r12), %r14d")
  __jited("movswl	0x18(%rax,%r12), %r14d")
  __jited("movsbl	0x20(%rax,%r12), %r14d")
  __jited("movslq	0x10(%rdi,%r12), %r15d")
  __jited("movswl	0x18(%rdi,%r12), %r15d")
  __jited("movsbl	0x20(%rdi,%r12), %r15d")
  __naked void arena_ldsx_disasm(void *ctx)
  {
  	asm volatile (
  	"r1 = %[arena] ll;"
  	"r2 = 0;"
  	"r3 = 1;"
  	"r4 = %[numa_no_node];"
  	"r5 = 0;"
  	"call %[bpf_arena_alloc_pages];"
  	"r0 = addr_space_cast(r0, 0x0, 0x1);"
  	"r1 = r0;"
  	"r8 = *(s32 *)(r0 + 16);"
  	"r8 = *(s16 *)(r0 + 24);"
  	"r8 = *(s8  *)(r0 + 32);"
  	"r9 = *(s32 *)(r1 + 16);"
  	"r9 = *(s16 *)(r1 + 24);"
  	"r9 = *(s8  *)(r1 + 32);"
  	"r0 = 0;"
  	"exit;"
  	:: __imm(bpf_arena_alloc_pages),
  	   __imm_addr(arena),
  	   __imm_const(numa_no_node, NUMA_NO_NODE)
  	:  __clobber_all
  	);
  }

Disassembly shows instructions       movslq, movswl, movsbl.
While I'd expect that these shold be movslq, movswq, movsbq.
(Sign extend dword/word/byte to quad word).
These actually have different encodings, e.g.:

  46 0f be 74 20 20             movsbl  0x20(%rax,%r12), %r14d
  4e 0f be 74 20 20             movsbq  0x20(%rax,%r12), %r14

However, I can't conjure a test that shows a difference when loading and
sotring values. Is this just an instruction set oddity?

[...]

      parent reply	other threads:[~2025-05-14 21:13 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-14 17:54 [PATCH bpf-next v2] bpf, x86: Add support for signed arena loads Kumar Kartikeya Dwivedi
2025-05-14 19:25 ` Alexei Starovoitov
2025-05-14 19:37   ` Kumar Kartikeya Dwivedi
2025-05-14 21:13 ` Eduard Zingerman [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=m2y0uyc2v5.fsf@gmail.com \
    --to=eddyz87@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kernel-team@meta.com \
    --cc=kkd@meta.com \
    --cc=martin.lau@kernel.org \
    --cc=memxor@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 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.