netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf] bpf, libbpf: guard bpf inline asm from bpf_tail_call_static
@ 2020-10-21 20:32 Daniel Borkmann
  2020-10-21 20:49 ` Yonghong Song
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Daniel Borkmann @ 2020-10-21 20:32 UTC (permalink / raw)
  To: alexei.starovoitov
  Cc: bpf, netdev, yanivagman, yhs, andrii.nakryiko, Daniel Borkmann

Yaniv reported a compilation error after pulling latest libbpf:

  [...]
  ../libbpf/src/root/usr/include/bpf/bpf_helpers.h:99:10: error:
  unknown register name 'r0' in asm
                     : "r0", "r1", "r2", "r3", "r4", "r5");
  [...]

The issue got triggered given Yaniv was compiling tracing programs with native
target (e.g. x86) instead of BPF target, hence no BTF generated vmlinux.h nor
CO-RE used, and later llc with -march=bpf was invoked to compile from LLVM IR
to BPF object file. Given that clang was expecting x86 inline asm and not BPF
one the error complained that these regs don't exist on the former.

Guard bpf_tail_call_static() with defined(__bpf__) where BPF inline asm is valid
to use. BPF tracing programs on more modern kernels use BPF target anyway and
thus the bpf_tail_call_static() function will be available for them. BPF inline
asm is supported since clang 7 (clang <= 6 otherwise throws same above error),
and __bpf_unreachable() since clang 8, therefore include the latter condition
in order to prevent compilation errors for older clang versions. Given even an
old Ubuntu 18.04 LTS has official LLVM packages all the way up to llvm-10, I did
not bother to special case the __bpf_unreachable() inside bpf_tail_call_static()
further.

Fixes: 0e9f6841f664 ("bpf, libbpf: Add bpf_tail_call_static helper for bpf programs")
Reported-by: Yaniv Agman <yanivagman@gmail.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/CAMy7=ZUk08w5Gc2Z-EKi4JFtuUCaZYmE4yzhJjrExXpYKR4L8w@mail.gmail.com
---
 tools/lib/bpf/bpf_helpers.h | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h
index 2bdb7d6dbad2..72b251110c4d 100644
--- a/tools/lib/bpf/bpf_helpers.h
+++ b/tools/lib/bpf/bpf_helpers.h
@@ -72,6 +72,7 @@
 /*
  * Helper function to perform a tail call with a constant/immediate map slot.
  */
+#if __clang_major__ >= 8 && defined(__bpf__)
 static __always_inline void
 bpf_tail_call_static(void *ctx, const void *map, const __u32 slot)
 {
@@ -98,6 +99,7 @@ bpf_tail_call_static(void *ctx, const void *map, const __u32 slot)
 		     :: [ctx]"r"(ctx), [map]"r"(map), [slot]"i"(slot)
 		     : "r0", "r1", "r2", "r3", "r4", "r5");
 }
+#endif
 
 /*
  * Helper structure used by eBPF C program
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf] bpf, libbpf: guard bpf inline asm from bpf_tail_call_static
  2020-10-21 20:32 [PATCH bpf] bpf, libbpf: guard bpf inline asm from bpf_tail_call_static Daniel Borkmann
@ 2020-10-21 20:49 ` Yonghong Song
  2020-10-21 22:20 ` Yaniv Agman
  2020-10-21 23:18 ` Andrii Nakryiko
  2 siblings, 0 replies; 4+ messages in thread
From: Yonghong Song @ 2020-10-21 20:49 UTC (permalink / raw)
  To: Daniel Borkmann, alexei.starovoitov
  Cc: bpf, netdev, yanivagman, andrii.nakryiko



On 10/21/20 1:32 PM, Daniel Borkmann wrote:
> Yaniv reported a compilation error after pulling latest libbpf:
> 
>    [...]
>    ../libbpf/src/root/usr/include/bpf/bpf_helpers.h:99:10: error:
>    unknown register name 'r0' in asm
>                       : "r0", "r1", "r2", "r3", "r4", "r5");
>    [...]
> 
> The issue got triggered given Yaniv was compiling tracing programs with native
> target (e.g. x86) instead of BPF target, hence no BTF generated vmlinux.h nor
> CO-RE used, and later llc with -march=bpf was invoked to compile from LLVM IR
> to BPF object file. Given that clang was expecting x86 inline asm and not BPF
> one the error complained that these regs don't exist on the former.
> 
> Guard bpf_tail_call_static() with defined(__bpf__) where BPF inline asm is valid
> to use. BPF tracing programs on more modern kernels use BPF target anyway and
> thus the bpf_tail_call_static() function will be available for them. BPF inline
> asm is supported since clang 7 (clang <= 6 otherwise throws same above error),
> and __bpf_unreachable() since clang 8, therefore include the latter condition
> in order to prevent compilation errors for older clang versions. Given even an
> old Ubuntu 18.04 LTS has official LLVM packages all the way up to llvm-10, I did
> not bother to special case the __bpf_unreachable() inside bpf_tail_call_static()
> further.
> 
> Fixes: 0e9f6841f664 ("bpf, libbpf: Add bpf_tail_call_static helper for bpf programs")
> Reported-by: Yaniv Agman <yanivagman@gmail.com>
> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
> Link: https://lore.kernel.org/bpf/CAMy7=ZUk08w5Gc2Z-EKi4JFtuUCaZYmE4yzhJjrExXpYKR4L8w@mail.gmail.com

Acked-by: Yonghong Song <yhs@fb.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf] bpf, libbpf: guard bpf inline asm from bpf_tail_call_static
  2020-10-21 20:32 [PATCH bpf] bpf, libbpf: guard bpf inline asm from bpf_tail_call_static Daniel Borkmann
  2020-10-21 20:49 ` Yonghong Song
@ 2020-10-21 22:20 ` Yaniv Agman
  2020-10-21 23:18 ` Andrii Nakryiko
  2 siblings, 0 replies; 4+ messages in thread
From: Yaniv Agman @ 2020-10-21 22:20 UTC (permalink / raw)
  To: Daniel Borkmann
  Cc: alexei.starovoitov, bpf, netdev, Yonghong Song, Andrii Nakryiko

‫בתאריך יום ד׳, 21 באוק׳ 2020 ב-23:33 מאת ‪Daniel Borkmann‬‏
<‪daniel@iogearbox.net‬‏>:‬
>
> Yaniv reported a compilation error after pulling latest libbpf:
>
>   [...]
>   ../libbpf/src/root/usr/include/bpf/bpf_helpers.h:99:10: error:
>   unknown register name 'r0' in asm
>                      : "r0", "r1", "r2", "r3", "r4", "r5");
>   [...]
>
> The issue got triggered given Yaniv was compiling tracing programs with native
> target (e.g. x86) instead of BPF target, hence no BTF generated vmlinux.h nor
> CO-RE used, and later llc with -march=bpf was invoked to compile from LLVM IR
> to BPF object file. Given that clang was expecting x86 inline asm and not BPF
> one the error complained that these regs don't exist on the former.
>
> Guard bpf_tail_call_static() with defined(__bpf__) where BPF inline asm is valid
> to use. BPF tracing programs on more modern kernels use BPF target anyway and
> thus the bpf_tail_call_static() function will be available for them. BPF inline
> asm is supported since clang 7 (clang <= 6 otherwise throws same above error),
> and __bpf_unreachable() since clang 8, therefore include the latter condition
> in order to prevent compilation errors for older clang versions. Given even an
> old Ubuntu 18.04 LTS has official LLVM packages all the way up to llvm-10, I did
> not bother to special case the __bpf_unreachable() inside bpf_tail_call_static()
> further.
>
> Fixes: 0e9f6841f664 ("bpf, libbpf: Add bpf_tail_call_static helper for bpf programs")
> Reported-by: Yaniv Agman <yanivagman@gmail.com>
> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
> Link: https://lore.kernel.org/bpf/CAMy7=ZUk08w5Gc2Z-EKi4JFtuUCaZYmE4yzhJjrExXpYKR4L8w@mail.gmail.com
> ---
>  tools/lib/bpf/bpf_helpers.h | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h
> index 2bdb7d6dbad2..72b251110c4d 100644
> --- a/tools/lib/bpf/bpf_helpers.h
> +++ b/tools/lib/bpf/bpf_helpers.h
> @@ -72,6 +72,7 @@
>  /*
>   * Helper function to perform a tail call with a constant/immediate map slot.
>   */
> +#if __clang_major__ >= 8 && defined(__bpf__)
>  static __always_inline void
>  bpf_tail_call_static(void *ctx, const void *map, const __u32 slot)
>  {
> @@ -98,6 +99,7 @@ bpf_tail_call_static(void *ctx, const void *map, const __u32 slot)
>                      :: [ctx]"r"(ctx), [map]"r"(map), [slot]"i"(slot)
>                      : "r0", "r1", "r2", "r3", "r4", "r5");
>  }
> +#endif
>
>  /*
>   * Helper structure used by eBPF C program
> --
> 2.17.1
>

Tested-by: Yaniv Agman <yanivagman@gmail.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf] bpf, libbpf: guard bpf inline asm from bpf_tail_call_static
  2020-10-21 20:32 [PATCH bpf] bpf, libbpf: guard bpf inline asm from bpf_tail_call_static Daniel Borkmann
  2020-10-21 20:49 ` Yonghong Song
  2020-10-21 22:20 ` Yaniv Agman
@ 2020-10-21 23:18 ` Andrii Nakryiko
  2 siblings, 0 replies; 4+ messages in thread
From: Andrii Nakryiko @ 2020-10-21 23:18 UTC (permalink / raw)
  To: Daniel Borkmann
  Cc: Alexei Starovoitov, bpf, Networking, Yaniv Agman, Yonghong Song

On Wed, Oct 21, 2020 at 1:33 PM Daniel Borkmann <daniel@iogearbox.net> wrote:
>
> Yaniv reported a compilation error after pulling latest libbpf:
>
>   [...]
>   ../libbpf/src/root/usr/include/bpf/bpf_helpers.h:99:10: error:
>   unknown register name 'r0' in asm
>                      : "r0", "r1", "r2", "r3", "r4", "r5");
>   [...]
>
> The issue got triggered given Yaniv was compiling tracing programs with native
> target (e.g. x86) instead of BPF target, hence no BTF generated vmlinux.h nor
> CO-RE used, and later llc with -march=bpf was invoked to compile from LLVM IR
> to BPF object file. Given that clang was expecting x86 inline asm and not BPF
> one the error complained that these regs don't exist on the former.
>
> Guard bpf_tail_call_static() with defined(__bpf__) where BPF inline asm is valid
> to use. BPF tracing programs on more modern kernels use BPF target anyway and
> thus the bpf_tail_call_static() function will be available for them. BPF inline
> asm is supported since clang 7 (clang <= 6 otherwise throws same above error),
> and __bpf_unreachable() since clang 8, therefore include the latter condition
> in order to prevent compilation errors for older clang versions. Given even an
> old Ubuntu 18.04 LTS has official LLVM packages all the way up to llvm-10, I did
> not bother to special case the __bpf_unreachable() inside bpf_tail_call_static()
> further.
>
> Fixes: 0e9f6841f664 ("bpf, libbpf: Add bpf_tail_call_static helper for bpf programs")
> Reported-by: Yaniv Agman <yanivagman@gmail.com>
> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
> Link: https://lore.kernel.org/bpf/CAMy7=ZUk08w5Gc2Z-EKi4JFtuUCaZYmE4yzhJjrExXpYKR4L8w@mail.gmail.com
> ---

LGTM!

Acked-by: Andrii Nakryiko <andrii@kernel.org>

>  tools/lib/bpf/bpf_helpers.h | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h
> index 2bdb7d6dbad2..72b251110c4d 100644
> --- a/tools/lib/bpf/bpf_helpers.h
> +++ b/tools/lib/bpf/bpf_helpers.h
> @@ -72,6 +72,7 @@
>  /*
>   * Helper function to perform a tail call with a constant/immediate map slot.
>   */
> +#if __clang_major__ >= 8 && defined(__bpf__)
>  static __always_inline void
>  bpf_tail_call_static(void *ctx, const void *map, const __u32 slot)
>  {
> @@ -98,6 +99,7 @@ bpf_tail_call_static(void *ctx, const void *map, const __u32 slot)
>                      :: [ctx]"r"(ctx), [map]"r"(map), [slot]"i"(slot)
>                      : "r0", "r1", "r2", "r3", "r4", "r5");
>  }
> +#endif
>
>  /*
>   * Helper structure used by eBPF C program
> --
> 2.17.1
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-10-21 23:19 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-21 20:32 [PATCH bpf] bpf, libbpf: guard bpf inline asm from bpf_tail_call_static Daniel Borkmann
2020-10-21 20:49 ` Yonghong Song
2020-10-21 22:20 ` Yaniv Agman
2020-10-21 23:18 ` Andrii Nakryiko

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).