linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] libbpf: define bpf_tail_call_static when __clang__ is not defined
@ 2022-09-09 22:45 James Hilliard
  2022-09-10  0:26 ` Andrii Nakryiko
  0 siblings, 1 reply; 4+ messages in thread
From: James Hilliard @ 2022-09-09 22:45 UTC (permalink / raw)
  To: bpf
  Cc: James Hilliard, Daniel Borkmann, Alexei Starovoitov,
	Andrii Nakryiko, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
	Nathan Chancellor, Nick Desaulniers, Tom Rix, linux-kernel, llvm

The bpf_tail_call_static function is currently not defined unless
using clang >= 8.

To support bpf_tail_call_static on GCC we can check if __clang__ is
not defined to enable bpf_tail_call_static.

Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
---
 tools/lib/bpf/bpf_helpers.h | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h
index 7349b16b8e2f..30fc95e7cd76 100644
--- a/tools/lib/bpf/bpf_helpers.h
+++ b/tools/lib/bpf/bpf_helpers.h
@@ -131,7 +131,7 @@
 /*
  * Helper function to perform a tail call with a constant/immediate map slot.
  */
-#if __clang_major__ >= 8 && defined(__bpf__)
+#if (!defined(__clang__) || __clang_major__ >= 8) && defined(__bpf__)
 static __always_inline void
 bpf_tail_call_static(void *ctx, const void *map, const __u32 slot)
 {
@@ -139,8 +139,8 @@ bpf_tail_call_static(void *ctx, const void *map, const __u32 slot)
 		__bpf_unreachable();
 
 	/*
-	 * Provide a hard guarantee that LLVM won't optimize setting r2 (map
-	 * pointer) and r3 (constant map index) from _different paths_ ending
+	 * Provide a hard guarantee that the compiler won't optimize setting r2
+	 * (map pointer) and r3 (constant map index) from _different paths_ ending
 	 * up at the _same_ call insn as otherwise we won't be able to use the
 	 * jmpq/nopl retpoline-free patching by the x86-64 JIT in the kernel
 	 * given they mismatch. See also d2e4c1e6c294 ("bpf: Constant map key
@@ -148,8 +148,8 @@ bpf_tail_call_static(void *ctx, const void *map, const __u32 slot)
 	 *
 	 * Note on clobber list: we need to stay in-line with BPF calling
 	 * convention, so even if we don't end up using r0, r4, r5, we need
-	 * to mark them as clobber so that LLVM doesn't end up using them
-	 * before / after the call.
+	 * to mark them as clobber so that the compiler doesn't end up using
+	 * them before / after the call.
 	 */
 	asm volatile("r1 = %[ctx]\n\t"
 		     "r2 = %[map]\n\t"
-- 
2.34.1


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

* Re: [PATCH] libbpf: define bpf_tail_call_static when __clang__ is not defined
  2022-09-09 22:45 [PATCH] libbpf: define bpf_tail_call_static when __clang__ is not defined James Hilliard
@ 2022-09-10  0:26 ` Andrii Nakryiko
  2022-09-11  0:06   ` Yonghong Song
  2022-09-12  6:44   ` James Hilliard
  0 siblings, 2 replies; 4+ messages in thread
From: Andrii Nakryiko @ 2022-09-10  0:26 UTC (permalink / raw)
  To: James Hilliard
  Cc: bpf, Daniel Borkmann, Alexei Starovoitov, Andrii Nakryiko,
	Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
	Nathan Chancellor, Nick Desaulniers, Tom Rix, linux-kernel, llvm

On Fri, Sep 9, 2022 at 3:46 PM James Hilliard <james.hilliard1@gmail.com> wrote:
>
> The bpf_tail_call_static function is currently not defined unless
> using clang >= 8.
>
> To support bpf_tail_call_static on GCC we can check if __clang__ is
> not defined to enable bpf_tail_call_static.
>
> Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
> ---
>  tools/lib/bpf/bpf_helpers.h | 10 +++++-----
>  1 file changed, 5 insertions(+), 5 deletions(-)
>
> diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h
> index 7349b16b8e2f..30fc95e7cd76 100644
> --- a/tools/lib/bpf/bpf_helpers.h
> +++ b/tools/lib/bpf/bpf_helpers.h
> @@ -131,7 +131,7 @@
>  /*
>   * Helper function to perform a tail call with a constant/immediate map slot.
>   */
> -#if __clang_major__ >= 8 && defined(__bpf__)
> +#if (!defined(__clang__) || __clang_major__ >= 8) && defined(__bpf__)
>  static __always_inline void
>  bpf_tail_call_static(void *ctx, const void *map, const __u32 slot)
>  {
> @@ -139,8 +139,8 @@ bpf_tail_call_static(void *ctx, const void *map, const __u32 slot)
>                 __bpf_unreachable();
>
>         /*
> -        * Provide a hard guarantee that LLVM won't optimize setting r2 (map
> -        * pointer) and r3 (constant map index) from _different paths_ ending
> +        * Provide a hard guarantee that the compiler won't optimize setting r2
> +        * (map pointer) and r3 (constant map index) from _different paths_ ending
>          * up at the _same_ call insn as otherwise we won't be able to use the
>          * jmpq/nopl retpoline-free patching by the x86-64 JIT in the kernel
>          * given they mismatch. See also d2e4c1e6c294 ("bpf: Constant map key
> @@ -148,8 +148,8 @@ bpf_tail_call_static(void *ctx, const void *map, const __u32 slot)
>          *
>          * Note on clobber list: we need to stay in-line with BPF calling
>          * convention, so even if we don't end up using r0, r4, r5, we need
> -        * to mark them as clobber so that LLVM doesn't end up using them
> -        * before / after the call.
> +        * to mark them as clobber so that the compiler doesn't end up using
> +        * them before / after the call.
>          */
>         asm volatile("r1 = %[ctx]\n\t"
>                      "r2 = %[map]\n\t"

will this compile as is on GCC-BPF? I'm trying to understand what's
the point. Once GCC supports this ASM syntax we can add similar check
to __clang_major__, instead of allowing it for all GCC versions?

We must have done __clang_major__ check for a reason, old Clangs
probably had some problems compiling this. Maybe Daniel remembers?

> --
> 2.34.1
>

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

* Re: [PATCH] libbpf: define bpf_tail_call_static when __clang__ is not defined
  2022-09-10  0:26 ` Andrii Nakryiko
@ 2022-09-11  0:06   ` Yonghong Song
  2022-09-12  6:44   ` James Hilliard
  1 sibling, 0 replies; 4+ messages in thread
From: Yonghong Song @ 2022-09-11  0:06 UTC (permalink / raw)
  To: Andrii Nakryiko, James Hilliard
  Cc: bpf, Daniel Borkmann, Alexei Starovoitov, Andrii Nakryiko,
	Martin KaFai Lau, Song Liu, John Fastabend, KP Singh,
	Stanislav Fomichev, Hao Luo, Jiri Olsa, Nathan Chancellor,
	Nick Desaulniers, Tom Rix, linux-kernel, llvm



On 9/9/22 5:26 PM, Andrii Nakryiko wrote:
> On Fri, Sep 9, 2022 at 3:46 PM James Hilliard <james.hilliard1@gmail.com> wrote:
>>
>> The bpf_tail_call_static function is currently not defined unless
>> using clang >= 8.
>>
>> To support bpf_tail_call_static on GCC we can check if __clang__ is
>> not defined to enable bpf_tail_call_static.
>>
>> Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
>> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
>> ---
>>   tools/lib/bpf/bpf_helpers.h | 10 +++++-----
>>   1 file changed, 5 insertions(+), 5 deletions(-)
>>
>> diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h
>> index 7349b16b8e2f..30fc95e7cd76 100644
>> --- a/tools/lib/bpf/bpf_helpers.h
>> +++ b/tools/lib/bpf/bpf_helpers.h
>> @@ -131,7 +131,7 @@
>>   /*
>>    * Helper function to perform a tail call with a constant/immediate map slot.
>>    */
>> -#if __clang_major__ >= 8 && defined(__bpf__)
>> +#if (!defined(__clang__) || __clang_major__ >= 8) && defined(__bpf__)
>>   static __always_inline void
>>   bpf_tail_call_static(void *ctx, const void *map, const __u32 slot)
>>   {
>> @@ -139,8 +139,8 @@ bpf_tail_call_static(void *ctx, const void *map, const __u32 slot)
>>                  __bpf_unreachable();
>>
>>          /*
>> -        * Provide a hard guarantee that LLVM won't optimize setting r2 (map
>> -        * pointer) and r3 (constant map index) from _different paths_ ending
>> +        * Provide a hard guarantee that the compiler won't optimize setting r2
>> +        * (map pointer) and r3 (constant map index) from _different paths_ ending
>>           * up at the _same_ call insn as otherwise we won't be able to use the
>>           * jmpq/nopl retpoline-free patching by the x86-64 JIT in the kernel
>>           * given they mismatch. See also d2e4c1e6c294 ("bpf: Constant map key
>> @@ -148,8 +148,8 @@ bpf_tail_call_static(void *ctx, const void *map, const __u32 slot)
>>           *
>>           * Note on clobber list: we need to stay in-line with BPF calling
>>           * convention, so even if we don't end up using r0, r4, r5, we need
>> -        * to mark them as clobber so that LLVM doesn't end up using them
>> -        * before / after the call.
>> +        * to mark them as clobber so that the compiler doesn't end up using
>> +        * them before / after the call.
>>           */
>>          asm volatile("r1 = %[ctx]\n\t"
>>                       "r2 = %[map]\n\t"
> 
> will this compile as is on GCC-BPF? I'm trying to understand what's
> the point. Once GCC supports this ASM syntax we can add similar check
> to __clang_major__, instead of allowing it for all GCC versions?
> 
> We must have done __clang_major__ check for a reason, old Clangs
> probably had some problems compiling this. Maybe Daniel remembers?

Yes, clang >= 8 is needed to ensure 'slot' is propagated as 'const' 
instead of variables. clang 6 added support for inline asm but clang 8
is needed for above 'const' propagation into asm code.

> 
>> --
>> 2.34.1
>>

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

* Re: [PATCH] libbpf: define bpf_tail_call_static when __clang__ is not defined
  2022-09-10  0:26 ` Andrii Nakryiko
  2022-09-11  0:06   ` Yonghong Song
@ 2022-09-12  6:44   ` James Hilliard
  1 sibling, 0 replies; 4+ messages in thread
From: James Hilliard @ 2022-09-12  6:44 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: bpf, Daniel Borkmann, Alexei Starovoitov, Andrii Nakryiko,
	Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
	Nathan Chancellor, Nick Desaulniers, Tom Rix, linux-kernel, llvm

On Fri, Sep 9, 2022 at 6:26 PM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Fri, Sep 9, 2022 at 3:46 PM James Hilliard <james.hilliard1@gmail.com> wrote:
> >
> > The bpf_tail_call_static function is currently not defined unless
> > using clang >= 8.
> >
> > To support bpf_tail_call_static on GCC we can check if __clang__ is
> > not defined to enable bpf_tail_call_static.
> >
> > Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
> > Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
> > ---
> >  tools/lib/bpf/bpf_helpers.h | 10 +++++-----
> >  1 file changed, 5 insertions(+), 5 deletions(-)
> >
> > diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h
> > index 7349b16b8e2f..30fc95e7cd76 100644
> > --- a/tools/lib/bpf/bpf_helpers.h
> > +++ b/tools/lib/bpf/bpf_helpers.h
> > @@ -131,7 +131,7 @@
> >  /*
> >   * Helper function to perform a tail call with a constant/immediate map slot.
> >   */
> > -#if __clang_major__ >= 8 && defined(__bpf__)
> > +#if (!defined(__clang__) || __clang_major__ >= 8) && defined(__bpf__)
> >  static __always_inline void
> >  bpf_tail_call_static(void *ctx, const void *map, const __u32 slot)
> >  {
> > @@ -139,8 +139,8 @@ bpf_tail_call_static(void *ctx, const void *map, const __u32 slot)
> >                 __bpf_unreachable();
> >
> >         /*
> > -        * Provide a hard guarantee that LLVM won't optimize setting r2 (map
> > -        * pointer) and r3 (constant map index) from _different paths_ ending
> > +        * Provide a hard guarantee that the compiler won't optimize setting r2
> > +        * (map pointer) and r3 (constant map index) from _different paths_ ending
> >          * up at the _same_ call insn as otherwise we won't be able to use the
> >          * jmpq/nopl retpoline-free patching by the x86-64 JIT in the kernel
> >          * given they mismatch. See also d2e4c1e6c294 ("bpf: Constant map key
> > @@ -148,8 +148,8 @@ bpf_tail_call_static(void *ctx, const void *map, const __u32 slot)
> >          *
> >          * Note on clobber list: we need to stay in-line with BPF calling
> >          * convention, so even if we don't end up using r0, r4, r5, we need
> > -        * to mark them as clobber so that LLVM doesn't end up using them
> > -        * before / after the call.
> > +        * to mark them as clobber so that the compiler doesn't end up using
> > +        * them before / after the call.
> >          */
> >         asm volatile("r1 = %[ctx]\n\t"
> >                      "r2 = %[map]\n\t"
>
> will this compile as is on GCC-BPF? I'm trying to understand what's
> the point. Once GCC supports this ASM syntax we can add similar check
> to __clang_major__, instead of allowing it for all GCC versions?

So, it's a bit different since the ASM syntax issue is more of an
issue with GAS,
we would need a check along these lines I think for a proper migration path:
https://lore.kernel.org/bpf/20220912063514.2824432-1-james.hilliard1@gmail.com/

We would set something like once GAS support for the llvm syntax is added:
.ifdef .gasversion. < 24000

I don't think we can do a compiler version check here like with llvm since GCC
release cycles are out of sync with GAS(llvm assembler is kept in sync with the
compiler AFAIU).

>
> We must have done __clang_major__ check for a reason, old Clangs
> probably had some problems compiling this. Maybe Daniel remembers?
>
> > --
> > 2.34.1
> >

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

end of thread, other threads:[~2022-09-12  6:44 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-09 22:45 [PATCH] libbpf: define bpf_tail_call_static when __clang__ is not defined James Hilliard
2022-09-10  0:26 ` Andrii Nakryiko
2022-09-11  0:06   ` Yonghong Song
2022-09-12  6:44   ` James Hilliard

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