From: Eduard Zingerman <eddyz87@gmail.com>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>,
"Jose E. Marchesi" <jemarch@gnu.org>,
"Jose E. Marchesi" <jose.marchesi@oracle.com>
Cc: Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Yonghong Song <yonghong.song@linux.dev>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Martin KaFai Lau <martin.lau@kernel.org>,
Daniel Xu <dxu@dxuuu.xyz>,
John Fastabend <john.fastabend@gmail.com>,
bpf <bpf@vger.kernel.org>, Kernel Team <kernel-team@fb.com>
Subject: Re: asm register constraint. Was: [PATCH v2 bpf-next 2/5] bpf: Introduce "volatile compare" macro
Date: Thu, 11 Jan 2024 18:46:01 +0200 [thread overview]
Message-ID: <fb2f414525573af672e67c51ff18b4725fc9ba64.camel@gmail.com> (raw)
In-Reply-To: <CAADnVQLmXxn9RrniktuW80XO14oyOmgJ_NboBBC_-CU4O=-v9g@mail.gmail.com>
On Mon, 2024-01-08 at 13:33 -0800, Alexei Starovoitov wrote:
[...]
> Agree that llvm fix [6] is a necessary step, then
> using 'w' in v3/v4 and warn on v1/v2 makes sense too,
> but we have this macro:
> #define barrier_var(var) asm volatile("" : "+r"(var))
> that is used in various bpf production programs.
> tetragon is also a heavy user of inline asm.
I have an llvm version [1] that implements 32-bit/64-bit register
selection and it indeed breaks some code when tested on BPF selftests.
The main pain point is that code base is compiled both with and
without ALU32, so 'r' constraint cannot be used with 'int'
(and constants are 'int' by default), hence the fixes like:
size_t off = (size_t)buf->head;
- asm("%0 -= %1" : "+r"(off) : "r"(buf->skb->data));
+ asm("%0 -= %1" : "+r"(off) : "r"((__u64)buf->skb->data));
return off;
or
#ifndef bpf_nop_mov
#define bpf_nop_mov(var) \
- asm volatile("%[reg]=%[reg]"::[reg]"r"((short)var))
+ asm volatile("%[reg]=%[reg]"::[reg]"r"((unsigned long)var))
#endif
or
int save_syn = 1;
int rv = -1;
int v = 0;
- int op;
+ long op;
...
asm volatile (
"%[op] = *(u32 *)(%[skops] +96)"
: [op] "+r"(op)
: [skops] "r"(skops)
:);
[1] https://github.com/eddyz87/llvm-project/tree/bpf-inline-asm-polymorphic-r
Need a bit more time to share the branch with updates for selftests.
> And, the most importantly, we need a way to go back to old behavior,
> since u32 var; asm("...":: "r"(var)); will now
> allocate "w" register or warn.
>
> What should be the workaround?
The u64 cast is the workaround.
> I've tried:
> u32 var; asm("...":: "r"((u64)var));
>
> https://godbolt.org/z/n4ejvWj7v
>
> and x86/arm64 generate 32-bit truction.
> Sounds like the bpf backend has to do it as well.
Here is godbolt output:
callq bar()@PLT
movl %eax, %eax ; <-- (u64)var is translated as zero extension
movq %rax, %rax ; <-- inline asm block uses 64-bit register
Here is LLVM IR for this example before code generation:
%call = tail call i64 @bar() #2
%conv1 = and i64 %call, 4294967295 ; <------- `(u64)var` translation
tail call void asm sideeffect "mov $0, $0",
"r,~{dirflag},~{fpsr},~{flags}"(i64 %conv1) #2
ret void
> We should be doing 'wX=wX' in such case (just like x86)
> instead of <<=32 >>=32.
Opened pull request for this:
https://github.com/llvm/llvm-project/pull/77501
> We probably need some macro (like we did for __BPF_CPU_VERSION__)
> to identify such fixed llvm, so existing users with '(short)'
> workaround and other tricks can detect new vs old compiler.
>
> Looks like we opened a can of worms.
> Aligning with x86/arm64 makes sense, but the cost of doing
> the right thing is hard to estimate.
Indeed.
next prev parent reply other threads:[~2024-01-11 16:46 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-12-21 3:38 [PATCH v2 bpf-next 0/5] bpf: volatile compare Alexei Starovoitov
2023-12-21 3:38 ` [PATCH v2 bpf-next 1/5] selftests/bpf: Attempt to build BPF programs with -Wsign-compare Alexei Starovoitov
2023-12-21 3:38 ` [PATCH v2 bpf-next 2/5] bpf: Introduce "volatile compare" macro Alexei Starovoitov
2023-12-21 4:27 ` Kumar Kartikeya Dwivedi
2023-12-22 22:59 ` Alexei Starovoitov
2023-12-25 20:33 ` Alexei Starovoitov
2024-01-04 20:06 ` Eduard Zingerman
2024-01-04 21:02 ` Alexei Starovoitov
2024-01-05 21:47 ` Eduard Zingerman
2024-01-08 21:21 ` Yonghong Song
2024-01-08 23:16 ` Eduard Zingerman
2024-01-08 21:33 ` asm register constraint. Was: " Alexei Starovoitov
2024-01-08 23:22 ` Yonghong Song
2024-01-09 10:49 ` Jose E. Marchesi
2024-01-09 12:09 ` Jose E. Marchesi
2024-01-11 18:33 ` Jose E. Marchesi
2024-01-15 16:33 ` Eduard Zingerman
2024-01-16 17:47 ` Alexei Starovoitov
2024-01-16 19:07 ` Yonghong Song
2024-01-16 19:34 ` Alexei Starovoitov
2024-01-16 23:14 ` Yonghong Song
2024-01-17 22:43 ` Alexei Starovoitov
2024-01-16 23:55 ` Eduard Zingerman
2024-01-16 18:40 ` Alexei Starovoitov
2024-01-16 23:15 ` Eduard Zingerman
2024-01-11 2:46 ` Alexei Starovoitov
2024-01-11 10:34 ` Jose E. Marchesi
2024-01-11 16:46 ` Eduard Zingerman [this message]
2023-12-21 3:38 ` [PATCH v2 bpf-next 3/5] selftests/bpf: Convert exceptions_assert.c to bpf_cmp Alexei Starovoitov
2023-12-21 3:38 ` [PATCH v2 bpf-next 4/5] selftests/bpf: Remove bpf_assert_eq-like macros Alexei Starovoitov
2023-12-21 3:38 ` [RFC PATCH v2 bpf-next 5/5] selftests/bpf: Attempt to convert profiler.c to bpf_cmp Alexei Starovoitov
2023-12-21 18:01 ` [PATCH v2 bpf-next 0/5] bpf: volatile compare Jiri Olsa
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=fb2f414525573af672e67c51ff18b4725fc9ba64.camel@gmail.com \
--to=eddyz87@gmail.com \
--cc=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=dxu@dxuuu.xyz \
--cc=jemarch@gnu.org \
--cc=john.fastabend@gmail.com \
--cc=jose.marchesi@oracle.com \
--cc=kernel-team@fb.com \
--cc=martin.lau@kernel.org \
--cc=memxor@gmail.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