From: Yonghong Song <yonghong.song@linux.dev>
To: Eduard Zingerman <eddyz87@gmail.com>, bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
kernel-team@fb.com, Martin KaFai Lau <martin.lau@kernel.org>
Subject: Re: [PATCH bpf-next] selftests/bpf: Fix some incorrect inline asm codes
Date: Thu, 12 Jun 2025 12:22:35 -0700 [thread overview]
Message-ID: <40f68a53-a358-41a3-8cf4-cb0d61d43842@linux.dev> (raw)
In-Reply-To: <5341c8c05537d6f9a4d252f5c98ec895ade09430.camel@gmail.com>
On 6/12/25 12:10 PM, Eduard Zingerman wrote:
> On Thu, 2025-06-12 at 10:19 -0700, Yonghong Song wrote:
>> In one of upstream thread ([1]), there is a discussion about
>> the below inline asm code:
>>
>> if r1 == 0xdeadbeef goto +2;
>> ...
>>
>> In actual llvm backend, the above 0xdeadbeef will actually do
>> sign extension to 64bit value and then compare to register r1.
>>
>> But the code itself does not imply the above semantics. It looks
>> like the comparision is between r1 and 0xdeadbeef. For example,
>> let us at a simple C code:
>> $ cat t1.c
>> int foo(long a) { return a == 0xdeadbeef ? 2 : 3; }
>> $ clang --target=bpf -O2 -c t1.c && llvm-objdump -d t1.o
>> ...
>> w0 = 0x2
>> r2 = 0xdeadbeef ll
>> if r1 == r2 goto +0x1
>> w0 = 0x3
>> exit
>> It does try to compare r1 and 0xdeadbeef.
>>
>> To address the above confusing inline asm issue, llvm backend ([2])
>> added some range checking for such insns and beyond. For the above
>> insn asm, the warning like below
>> warning: immediate out of range, shall fit in int range
>> will be issued. If -Werror is in the compilation flags, the
>> error will be issued.
>>
>> To avoid the above warning/error, the afore-mentioned inline asm
>> should be rewritten to
>>
>> if r1 == -559038737 goto +2;
>> ...
>>
>> Fix a few selftest cases like the above based on insn range checking
>> requirement in [2].
>>
>> [1] https://lore.kernel.org/bpf/70affb12-327b-4882-bd1d-afda8b8c6f56@linux.dev/
>> [2] https://github.com/llvm/llvm-project/pull/142989
>>
>> Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
>> ---
> Changes like 0xffffffff -> -1 and 0xfffffffe -> -2 look fine,
> but changes like 0xffff1234 -> -60876 are an unnecessary obfuscation,
> maybe we need to reconsider.
Another option is to generate below in inline asm:
r2 = 0xffffFFFFffff1234 /* or r2 = 0xffff1234, depending on the actual user expectation */
if (r1 == r2) ...
>
> [...]
prev parent reply other threads:[~2025-06-12 19:22 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-12 17:19 [PATCH bpf-next] selftests/bpf: Fix some incorrect inline asm codes Yonghong Song
2025-06-12 17:59 ` Jose E. Marchesi
2025-06-12 19:10 ` Eduard Zingerman
2025-06-12 19:18 ` Alexei Starovoitov
2025-06-12 19:29 ` Yonghong Song
2025-06-12 19:49 ` Eduard Zingerman
2025-06-12 21:15 ` Alexei Starovoitov
2025-06-12 21:23 ` Eduard Zingerman
2025-06-12 21:39 ` Yonghong Song
2025-06-12 21:42 ` Alexei Starovoitov
2025-06-13 2:04 ` Yonghong Song
2025-06-12 19:22 ` Yonghong Song [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=40f68a53-a358-41a3-8cf4-cb0d61d43842@linux.dev \
--to=yonghong.song@linux.dev \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=kernel-team@fb.com \
--cc=martin.lau@kernel.org \
/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.