BPF List
 help / color / mirror / Atom feed
From: Yonghong Song <yhs@fb.com>
To: James Hilliard <james.hilliard1@gmail.com>, bpf@vger.kernel.org
Cc: Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Alexei Starovoitov <ast@kernel.org>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	Song Liu <song@kernel.org>,
	John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@kernel.org>,
	Stanislav Fomichev <sdf@google.com>, Hao Luo <haoluo@google.com>,
	Jiri Olsa <jolsa@kernel.org>,
	Nathan Chancellor <nathan@kernel.org>,
	Nick Desaulniers <ndesaulniers@google.com>,
	Tom Rix <trix@redhat.com>,
	linux-kernel@vger.kernel.org, llvm@lists.linux.dev
Subject: Re: [PATCH v2] libbpf: define bpf_tail_call_static when __clang__ is not defined
Date: Mon, 12 Sep 2022 18:29:33 +0100	[thread overview]
Message-ID: <7cb9ebd2-5ee1-f00e-5d8e-0fe454f975a0@fb.com> (raw)
In-Reply-To: <20220912063514.2824432-1-james.hilliard1@gmail.com>



On 9/12/22 7:35 AM, James Hilliard wrote:
> The bpf_tail_call_static function is currently not defined unless
> using clang >= 8.
> 
> To support bpf_tail_call_static on GAS we can check if __clang__ is
> not defined to enable bpf_tail_call_static.
> 
> We need to use a GAS assembly syntax check so that the assembler
> is provided GAS compatible assembly as well.
> 
> We can use gasversion to provide a migration path to llvm syntax
> for GAS once llvm syntax is natively supported.

I didn't see a gasversion comparison in asm code.
Is it possible that we compare gasversion to a known
gas version which supports new syntax? If the gasversion
is supported, use the same syntax as llvm. If the
gasversion is not supported, output an illegal insn
and it would be even better if some error information
is printed out on the screen.

> 
> Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
> ---
> Changes v1 -> v2:
>    - use gasversion to detect assembly variant
> ---
>   tools/lib/bpf/bpf_helpers.h | 18 ++++++++++++------
>   1 file changed, 12 insertions(+), 6 deletions(-)
> 
> diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h
> index 7349b16b8e2f..5b98f5506798 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,12 +148,18 @@ 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"
> +	asm volatile(".ifdef .gasversion.\n\t"
> +		     "mov %%r1,%[ctx]\n\t"
> +		     "mov %%r2,%[map]\n\t"
> +		     "mov %%r3,%[slot]\n\t"
> +		     ".else\n\t"
> +		     "r1 = %[ctx]\n\t"
>   		     "r2 = %[map]\n\t"
>   		     "r3 = %[slot]\n\t"
> +		     ".endif\n\t"
>   		     "call 12"
>   		     :: [ctx]"r"(ctx), [map]"r"(map), [slot]"i"(slot)
>   		     : "r0", "r1", "r2", "r3", "r4", "r5");

  reply	other threads:[~2022-09-12 17:30 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-12  6:35 [PATCH v2] libbpf: define bpf_tail_call_static when __clang__ is not defined James Hilliard
2022-09-12 17:29 ` Yonghong Song [this message]
2022-09-12 21:32   ` James Hilliard

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=7cb9ebd2-5ee1-f00e-5d8e-0fe454f975a0@fb.com \
    --to=yhs@fb.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=haoluo@google.com \
    --cc=james.hilliard1@gmail.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=martin.lau@linux.dev \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=sdf@google.com \
    --cc=song@kernel.org \
    --cc=trix@redhat.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox